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

Fixed type annotations for keys of multidict mapping classes #677

Merged
merged 4 commits into from
Jan 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/644.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed type annotations for keys of multidict mapping classes.
47 changes: 28 additions & 19 deletions multidict/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class istr(str): ...

upstr = istr

_S = Union[str, istr]
_S = TypeVar("_S", str, istr)

_T = TypeVar("_T")

Expand All @@ -39,13 +39,17 @@ class MultiMapping(Mapping[_S, _T_co]):
@abc.abstractmethod
def getone(self, key: _S, default: _D) -> Union[_T_co, _D]: ...

_Arg = Union[Mapping[_S, _T], Dict[_S, _T], MultiMapping[_T], Iterable[Tuple[_S, _T]]]
_Arg = Union[
Mapping[_S, _T], Dict[_S, _T], MultiMapping[_S, _T], Iterable[Tuple[_S, _T]]
]

class MutableMultiMapping(MultiMapping[_T], MutableMapping[_S, _T], Generic[_T]):
class MutableMultiMapping(
MultiMapping[_S, _T], MutableMapping[_S, _T], Generic[_S, _T]
):
@abc.abstractmethod
def add(self, key: _S, value: _T) -> None: ...
@abc.abstractmethod
def extend(self, arg: _Arg[_T] = ..., **kwargs: _T) -> None: ...
def extend(self, arg: _Arg[_S, _T] = ..., **kwargs: _T) -> None: ...
@overload
@abc.abstractmethod
def popone(self, key: _S) -> _T: ...
Expand All @@ -59,9 +63,9 @@ class MutableMultiMapping(MultiMapping[_T], MutableMapping[_S, _T], Generic[_T])
@abc.abstractmethod
def popall(self, key: _S, default: _D) -> Union[List[_T], _D]: ...

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

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

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

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

def getversion(
md: Union[MultiDict[_T], CIMultiDict[_T], MultiDictProxy[_T], CIMultiDictProxy[_T]]
md: Union[
MultiDict[_S, _T],
CIMultiDict[_S, _T],
MultiDictProxy[_S, _T],
CIMultiDictProxy[_S, _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]
d2 = multidict.CIMultiDict({"a": "b"}) # type: multidict.CIMultiDict[str]
d1 = multidict.MultiDict({"a": "b"}) # type: multidict.MultiDict[str,str]
d2 = multidict.CIMultiDict({"a": "b"}) # type: multidict.CIMultiDict[str,str]

d3 = multidict.MultiDictProxy(d1)
d4 = multidict.CIMultiDictProxy(d2)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class VersionMixin:
cls: Type[MultiMapping[str]]
cls: Type[MultiMapping[str, str]]

def getver(self, md):
raise NotImplementedError
Expand Down
2 changes: 1 addition & 1 deletion tools/check_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def main(argv):
if fname.name in (".gitignore", ".TEMPLATE.rst", "README.rst"):
continue
if fname.suffix == ".rst":
test_name = fname.stem
test_name = Path(fname.stem)
else:
test_name = fname
if test_name.suffix not in ALLOWED_SUFFIXES:
Expand Down