Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop wrong key parametrization for concrete classes #682

Merged
merged 4 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fails due to the _S. mypy now expects only 1 type in the annotation, but also wants 2 types to be defined (_S, _T), which appears to be impossible.

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