-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add support for dict.{keys,values,items}.mapping
#6039
Add support for dict.{keys,values,items}.mapping
#6039
Conversation
This comment has been minimized.
This comment has been minimized.
ac61939
to
9e2605a
Compare
This comment has been minimized.
This comment has been minimized.
490430a
to
f40d486
Compare
This comment has been minimized.
This comment has been minimized.
0957a1e
to
20e6fc0
Compare
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The SinbadCogs mypy-primer error looks concerning. |
20e6fc0
to
887daa7
Compare
This comment has been minimized.
This comment has been minimized.
Hi @JelleZijlstra, I think this is an issue with mypy, not with this change: from typing import TypeVar, Generic, ValuesView
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
class AValues(ValuesView[_VT_co], Generic[_KT_co, _VT_co]):
pass
class A(dict[_KT, _VT], Generic[_KT, _VT]):
def c(self) -> AValues[_KT, _VT]:
return AValues(self)
a = A({1: [1]})
l = [*a.c()]
print(l) # [[1]]
reveal_type(l) # note: Revealed type is "builtins.list[builtins.int*]" from typing import TypeVar, Generic, ValuesView
_KT = TypeVar("_KT")
_VT = TypeVar("_VT")
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
class AValues(ValuesView[_VT_co], Generic[_VT_co]):
pass
class A(dict[_KT, _VT], Generic[_KT, _VT]):
def c(self) -> AValues[_VT]:
return AValues(self)
a = A({1: [1]})
l = [*a.c()]
print(l) # [[1]]
reveal_type([*a.c()]) # note: Revealed type is "builtins.list[builtins.list*[builtins.int*]]" |
Mypy does seem buggy there, but I think you can work around the bug by swapping the type arguments to |
0fb471c
to
968c3c2
Compare
This comment has been minimized.
This comment has been minimized.
968c3c2
to
f3999dd
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
f3999dd
to
3587a3e
Compare
Hi @JelleZijlstra, just a small note: are the generics on _dict_items redundant? class _dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]):
# could be:
class _dict_items(ItemsView[_KT_co, _VT_co]): |
This comment has been minimized.
This comment has been minimized.
stdlib/collections/__init__.pyi
Outdated
class _OrderedDictKeysView(KeysView[_KT], Reversible[_KT]): | ||
class _OrderedDictKeysView(_dict_keys[_KT, _VT], Reversible[_KT]): | ||
def __reversed__(self) -> Iterator[_KT]: ... | ||
|
||
class _OrderedDictItemsView(ItemsView[_KT, _VT], Reversible[Tuple[_KT, _VT]]): | ||
class _OrderedDictItemsView(_dict_items[_KT, _VT], Reversible[Tuple[_KT, _VT]]): | ||
def __reversed__(self) -> Iterator[Tuple[_KT, _VT]]: ... | ||
|
||
class _OrderedDictValuesView(ValuesView[_VT], Reversible[_VT]): | ||
class _OrderedDictValuesView(_dict_values[_VT, _KT], Reversible[_VT], Generic[_KT, _VT]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the generics here be covariant? the base types are.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense: they are immutable, so a _dict_items[int, str]
should also be a valid _dict_items[object, object]
.
Nit: Please do not force-push or git commit --amend
, so that we can see what changed after your previous review. We will squash the commits when merging, so the whole PR will appear as a single commit in the Git history anyway.
This comment has been minimized.
This comment has been minimized.
3587a3e
to
0d11717
Compare
This comment has been minimized.
This comment has been minimized.
And yes, the generics on |
9c8b4d0
to
5fd7c9b
Compare
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: aioredis (https://github.com/aio-libs/aioredis.git)
- aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "KeysView[Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[bytes, float]]")
+ aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "_dict_keys[Any, Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[bytes, float]]")
- aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "KeysView[Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[str, float]]")
+ aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "_dict_keys[Any, Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[str, float]]")
- aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "KeysView[Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[memoryview, float]]")
+ aioredis/client.py:3401: error: Incompatible types in assignment (expression has type "_dict_keys[Any, Any]", variable has type "Union[Sequence[Union[bytes, str, memoryview]], Mapping[memoryview, float]]")
- aioredis/client.py:3403: error: Incompatible types in assignment (expression has type "None", variable has type "ValuesView[Any]")
+ aioredis/client.py:3403: error: Incompatible types in assignment (expression has type "None", variable has type "_dict_values[Any, Any]")
|
I think this is ready to pull. |
Source commit: python/typeshed@a9227ed Fixes for python/typeshed#6039 Co-authored-by: hauntsaninja <>
I added
_dict_keys
,_dict_values
,_dict_items
classes tobuiltins.pyi
and madedict
return those instead of the baseXView
.from: https://bugs.python.org/issue40890