Dictionary unpacking with object that doesn't inherit from collections.abc.Mapping
#1375
-
While writing the type stubs for from __future__ import annotations
from collections.abc import ItemsView, Iterator, KeysView, Mapping, ValuesView
from typing import Any
class MyMapping(Mapping[str, Any]):
def __getitem__(self, __key: str) -> str:
...
def __len__(self) -> int:
...
def __iter__(self) -> Iterator[Any]:
...
class Record:
def __getitem__(self, __key: str) -> str:
...
def get(self, __key: str, default: Any) -> Any:
...
def items(self) -> ItemsView[str, str]:
...
def keys(self) -> KeysView[str]:
...
def values(self) -> ValuesView[str]:
...
def __contains__(self, __o: object) -> bool:
...
def __len__(self) -> int:
...
def __iter__(self) -> Iterator[str]:
...
def some_func(**kwargs: Any) -> None:
...
m = MyMapping()
r = Record()
some_func(**m)
some_func(**r) # Error: Argument after ** must be a mapping, not "Record" Is this a bug in the current type checking logic of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
To my knowledge, there is no protocol class defined for the concept of I see a couple of potential solutions:
|
Beta Was this translation helpful? Give feedback.
-
Now that |
Beta Was this translation helpful? Give feedback.
Now that
mypy
has released support forSupportsKeysAndGetItem
being destructured in1.3
, the answer to this question is to upgrademypy
orpyright
and it will work. Thanks for the help, @AlexWaygood and @erictraut!