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:
fromcollections.abcimportItemsView, KeysView, ValuesViewfromtypingimportIteratorclassMap[KT, VT]:
def__init__(self, mapping: dict[KT, VT]) ->None:
self._items: dict[KT, VT] =mappingdef__getitem__(self, key: KT) ->VT:
returnself._items[key]
def__len__(self) ->int:
returnlen(self._items)
def__iter__(self) ->Iterator[KT]:
returniter(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.pyt.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)
In my case I couldn't add multiple inheritance with Mapping because of metaclass conflict
Maybe it's intended to be like that, but if some class implements all the necessary methods for
KeysView,ValuesVieworItemsView,mypywill generate errors, althoughpythoncode will work:In my case I couldn't add multiple inheritance with
Mappingbecause of metaclass conflict