Skip to content

Commit

Permalink
Drop wrong key parametrization for concrete classes (#682)
Browse files Browse the repository at this point in the history
* Drop wrong key paramtrization for concrete classes

* Add changelog

* Rename

* Reformat changelog
  • Loading branch information
asvetlov authored Jan 23, 2022
1 parent c7138ca commit 19c4ef3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 10 additions & 0 deletions CHANGES/682.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Restored back ``MultiDict``, ``CIMultiDict``, ``MultiDictProxy``, and
``CIMutiDictProxy`` generic type arguments; they are parametrized by value type, but the
key type is fixed by container class.

``MultiDict[int]`` means ``MutableMultiMapping[str, int]``. The key type of
``MultiDict`` is always ``str``, while all str-like keys are accepted by API and
converted to ``str`` internally.

The same is true for ``CIMultiDict[int]`` which means ``MutableMultiMapping[istr,
int]``. str-like keys are accepted but converted to ``istr`` internally.
28 changes: 14 additions & 14 deletions multidict/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class MutableMultiMapping(
@abc.abstractmethod
def popall(self, key: _S, default: _D) -> Union[List[_T], _D]: ...

class MultiDict(MutableMultiMapping[_S, _T], Generic[_S, _T]):
class MultiDict(MutableMultiMapping[str, _T]):
def __init__(self, arg: _Arg[_S, _T] = ..., **kwargs: _T) -> None: ...
def copy(self) -> MultiDict[_S, _T]: ...
def copy(self) -> MultiDict[_T]: ...
def __getitem__(self, k: _S) -> _T: ...
def __setitem__(self, k: _S, v: _T) -> None: ...
def __delitem__(self, v: _S) -> None: ...
Expand All @@ -90,9 +90,9 @@ class MultiDict(MutableMultiMapping[_S, _T], Generic[_S, _T]):
@overload
def popall(self, key: _S, default: _D) -> Union[List[_T], _D]: ...

class CIMultiDict(MutableMultiMapping[_S, _T], Generic[_S, _T]):
class CIMultiDict(MutableMultiMapping[istr, _T]):
def __init__(self, arg: _Arg[_S, _T] = ..., **kwargs: _T) -> None: ...
def copy(self) -> CIMultiDict[_S, _T]: ...
def copy(self) -> CIMultiDict[_T]: ...
def __getitem__(self, k: _S) -> _T: ...
def __setitem__(self, k: _S, v: _T) -> None: ...
def __delitem__(self, v: _S) -> None: ...
Expand All @@ -117,11 +117,11 @@ class CIMultiDict(MutableMultiMapping[_S, _T], Generic[_S, _T]):
@overload
def popall(self, key: _S, default: _D) -> Union[List[_T], _D]: ...

class MultiDictProxy(MultiMapping[_S, _T], Generic[_S, _T]):
class MultiDictProxy(MultiMapping[str, _T]):
def __init__(
self, arg: Union[MultiMapping[_S, _T], MutableMultiMapping[_S, _T]]
self, arg: Union[MultiDict[_T], MultiDictProxy[_T]]
) -> None: ...
def copy(self) -> MultiDict[_S, _T]: ...
def copy(self) -> MultiDict[_T]: ...
def __getitem__(self, k: _S) -> _T: ...
def __iter__(self) -> Iterator[_S]: ...
def __len__(self) -> int: ...
Expand All @@ -134,10 +134,11 @@ class MultiDictProxy(MultiMapping[_S, _T], Generic[_S, _T]):
@overload
def getone(self, key: _S, default: _D) -> Union[_T, _D]: ...

class CIMultiDictProxy(MultiMapping[_S, _T], Generic[_S, _T]):
class CIMultiDictProxy(MultiMapping[istr, _T]):
def __init__(
self, arg: Union[MultiMapping[_S, _T], MutableMultiMapping[_S, _T]]
self, arg: Union[CIMultiDict[_T], CIMultiDictProxy[_T]]
) -> None: ...
def copy(self) -> CIMultiDict[_T]: ...
def __getitem__(self, k: _S) -> _T: ...
def __iter__(self) -> Iterator[_S]: ...
def __len__(self) -> int: ...
Expand All @@ -149,13 +150,12 @@ class CIMultiDictProxy(MultiMapping[_S, _T], Generic[_S, _T]):
def getone(self, key: _S) -> _T: ...
@overload
def getone(self, key: _S, default: _D) -> Union[_T, _D]: ...
def copy(self) -> CIMultiDict[_S, _T]: ...

def getversion(
md: Union[
MultiDict[_S, _T],
CIMultiDict[_S, _T],
MultiDictProxy[_S, _T],
CIMultiDictProxy[_S, _T],
MultiDict[_T],
CIMultiDict[_T],
MultiDictProxy[_T],
CIMultiDictProxy[_T],
]
) -> int: ...
4 changes: 2 additions & 2 deletions tests/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


def test_classes_not_abstract() -> None:
d1 = multidict.MultiDict({"a": "b"}) # type: multidict.MultiDict[str,str]
d2 = multidict.CIMultiDict({"a": "b"}) # type: multidict.CIMultiDict[str,str]
d1: multidict.MultiDict[str] = multidict.MultiDict({"a": "b"})
d2: multidict.CIMultiDict[str] = multidict.CIMultiDict({"a": "b"})

d3 = multidict.MultiDictProxy(d1)
d4 = multidict.CIMultiDictProxy(d2)
Expand Down

0 comments on commit 19c4ef3

Please sign in to comment.