-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Description
Maybe it's intended to be like that, but if some class implements all the necessary methods for KeysView
, ValuesView
or ItemsView
, mypy
will generate errors, although python
code will work:
from collections.abc import ItemsView, KeysView, ValuesView
from typing import Iterator
class Map[KT, VT]:
def __init__(self, mapping: dict[KT, VT]) -> None:
self._items: dict[KT, VT] = mapping
def __getitem__(self, key: KT) -> VT:
return self._items[key]
def __len__(self) -> int:
return len(self._items)
def __iter__(self) -> Iterator[KT]:
return iter(self._items)
a = Map[int, int]({3: 4})
keys = KeysView(a)
values = ValuesView(a)
items = ItemsView(a)
print(list(keys))
print(len(keys))
$ mypy t.py
t.py:20: error: Need type annotation for "keys" [var-annotated]
t.py:20: error: Argument 1 to "KeysView" has incompatible type "Map[int, int]"; expected "Mapping[Never, Any]" [arg-type]
t.py:21: error: Need type annotation for "values" [var-annotated]
t.py:21: error: Argument 1 to "ValuesView" has incompatible type "Map[int, int]"; expected "Mapping[Any, Never]" [arg-type]
t.py:22: error: Need type annotation for "items" [var-annotated]
t.py:22: error: Argument 1 to "ItemsView" has incompatible type "Map[int, int]"; expected "Mapping[Never, Never]" [arg-type]
Found 6 errors in 1 file (checked 1 source file)
$ python3 t.py
[3]
1
In my case I couldn't add multiple inheritance with Mapping
because of metaclass conflict
Metadata
Metadata
Assignees
Labels
No labels