Skip to content

Commit f2f6943

Browse files
authored
Fix type hints (#220)
* Fix type hints * Tune mypy errors
1 parent c92f841 commit f2f6943

5 files changed

Lines changed: 61 additions & 27 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ rmcache:
2626

2727
mypy: .develop
2828
if python -c "import sys; sys.exit(sys.implementation.name != 'cpython')"; then \
29-
mypy multidict tests/test_mypy.py; \
29+
mypy multidict tests; \
3030
fi
3131

3232

multidict/__init__.pyi

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,13 @@ _S = Union[str, istr]
1212
_T = TypeVar('_T')
1313

1414

15-
class MultiDict(MutableMapping[_S, _T]):
16-
@overload
17-
def __init__(self) -> None: ...
18-
@overload
19-
def __init__(self, map: Mapping[_S, _T]) -> None: ...
20-
@overload
21-
def __init__(self, iterable: Iterable[Tuple[_S, _T]]) -> None: ...
22-
23-
def __getitem__(self, k: _S) -> _T: ...
24-
def __setitem__(self, k: _S, v: _T) -> None: ...
25-
def __delitem__(self, v: _S) -> None: ...
26-
def __iter__(self) -> Iterator[_S]: ...
27-
def __len__(self) -> int: ...
28-
15+
class MultiMapping(Mapping[_S, _T]):
2916
def getall(self, key: _S, default: _T=...) -> List[_T]: ...
3017
def getone(self, key: _S, default: _T=...) -> _T: ...
3118

32-
def copy(self) -> MultiDict: ...
19+
20+
class MutableMultiMapping(MultiMapping[_T],
21+
MutableMapping[_S, _T]):
3322

3423
def add(self, key: _S, value: _T) -> None: ...
3524

@@ -51,27 +40,61 @@ class MultiDict(MutableMapping[_S, _T]):
5140
def popall(self, key: _S, default: _T=...) -> List[_T]: ...
5241

5342

54-
class CIMultiDict(MultiDict[_T]):
55-
def copy(self) -> CIMultiDict[_T]: ...
43+
class MultiDict(MutableMultiMapping[_T]):
44+
@overload
45+
def __init__(self) -> None: ...
46+
@overload
47+
def __init__(self, map: Mapping[_S, _T]) -> None: ...
48+
@overload
49+
def __init__(self, iterable: Iterable[Tuple[_S, _T]]) -> None: ...
5650

51+
def copy(self) -> MultiDict[_T]: ...
5752

58-
class MultiDictProxy(Mapping[_S, _T]):
59-
def __init__(self, arg: Union[MultiDict[_T], MultiDictProxy[_T]]) -> None: ...
6053

6154
def __getitem__(self, k: _S) -> _T: ...
55+
def __setitem__(self, k: _S, v: _T) -> None: ...
56+
def __delitem__(self, v: _S) -> None: ...
6257
def __iter__(self) -> Iterator[_S]: ...
6358
def __len__(self) -> int: ...
6459

65-
def getall(self, key: _S, default: _T=...) -> List[_T]: ...
66-
def getone(self, key: _S, default: _T=...) -> _T: ...
6760

68-
def copy(self) -> MultiDictProxy: ...
61+
class CIMultiDict(MutableMultiMapping[_T]):
62+
@overload
63+
def __init__(self) -> None: ...
64+
@overload
65+
def __init__(self, map: Mapping[_S, _T]) -> None: ...
66+
@overload
67+
def __init__(self, iterable: Iterable[Tuple[_S, _T]]) -> None: ...
68+
69+
def copy(self) -> CIMultiDict[_T]: ...
70+
71+
def __getitem__(self, k: _S) -> _T: ...
72+
def __setitem__(self, k: _S, v: _T) -> None: ...
73+
def __delitem__(self, v: _S) -> None: ...
74+
def __iter__(self) -> Iterator[_S]: ...
75+
def __len__(self) -> int: ...
76+
6977

78+
class MultiDictProxy(MultiMapping[_T]):
79+
def __init__(self, arg: Union[MultiMapping[_T],
80+
MutableMultiMapping[_T]]) -> None: ...
81+
82+
def copy(self) -> MultiDictProxy[_T]: ...
83+
84+
def __getitem__(self, k: _S) -> _T: ...
85+
def __iter__(self) -> Iterator[_S]: ...
86+
def __len__(self) -> int: ...
7087

7188

72-
class CIMultiDictProxy(MultiDictProxy[_T]):
89+
class CIMultiDictProxy(MultiMapping[_T]):
90+
def __init__(self, arg: Union[MultiMapping[_T],
91+
MutableMultiMapping[_T]]) -> None: ...
7392
def copy(self) -> CIMultiDictProxy[_T]: ...
7493

94+
def __getitem__(self, k: _S) -> _T: ...
95+
def __iter__(self) -> Iterator[_S]: ...
96+
def __len__(self) -> int: ...
97+
7598

7699
def getversion(md: Union[MultiDict[_T],
77100
CIMultiDict[_T],

setup.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ test = pytest
44
[metadata]
55
license_file = LICENSE
66
long_description = file: README.rst
7+
8+
9+
[mypy-pytest]
10+
ignore_missing_imports = true
11+
12+
13+
[mypy-multidict._multidict]
14+
ignore_missing_imports = true

tests/test_istr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
from multidict._multidict_py import istr as _istr # noqa: E402
1212

1313

14+
IMPLEMENTATION = getattr(sys, 'implementation') # to suppress mypy error
15+
16+
1417
class IStrMixin:
1518

16-
cls = None
19+
cls = NotImplemented
1720

1821
def test_ctor(self):
1922
s = self.cls()
@@ -67,7 +70,7 @@ def _create_strs():
6770
istr2 = _istr()
6871
_istr(istr2)
6972

70-
@pytest.mark.skipif(sys.implementation.name != 'cpython',
73+
@pytest.mark.skipif(IMPLEMENTATION.name != 'cpython',
7174
reason="PyPy has different GC implementation")
7275
def test_leak(self):
7376
gc.collect()

tests/test_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class VersionMixin:
14-
cls = None
14+
cls = NotImplemented
1515

1616
def getver(self, md):
1717
raise NotImplementedError

0 commit comments

Comments
 (0)