Skip to content

Commit 1b5c4e1

Browse files
authored
Add support for dict.{keys,values,items}.mapping (#6039)
Co-authored-by: KotlinIsland <[email protected]>
1 parent 75c75a0 commit 1b5c4e1

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

stdlib/builtins.pyi

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ from _typeshed import (
2121
)
2222
from ast import AST, mod
2323
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
24-
from types import CodeType, TracebackType
24+
from types import CodeType, MappingProxyType, TracebackType
2525
from typing import (
2626
IO,
2727
AbstractSet,
@@ -73,6 +73,8 @@ _T_co = TypeVar("_T_co", covariant=True)
7373
_T_contra = TypeVar("_T_contra", contravariant=True)
7474
_KT = TypeVar("_KT")
7575
_VT = TypeVar("_VT")
76+
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
77+
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
7678
_S = TypeVar("_S")
7779
_T1 = TypeVar("_T1")
7880
_T2 = TypeVar("_T2")
@@ -787,6 +789,20 @@ class list(MutableSequence[_T], Generic[_T]):
787789
if sys.version_info >= (3, 9):
788790
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
789791

792+
class _dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]):
793+
if sys.version_info >= (3, 10):
794+
mapping: MappingProxyType[_KT_co, _VT_co]
795+
796+
# The generics are the wrong way around because of a mypy limitation
797+
# https://github.com/python/mypy/issues/11138
798+
class _dict_values(ValuesView[_VT_co], Generic[_VT_co, _KT_co]):
799+
if sys.version_info >= (3, 10):
800+
mapping: MappingProxyType[_KT_co, _VT_co]
801+
802+
class _dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]):
803+
if sys.version_info >= (3, 10):
804+
mapping: MappingProxyType[_KT_co, _VT_co]
805+
790806
class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
791807
@overload
792808
def __init__(self: dict[_KT, _VT]) -> None: ...
@@ -807,9 +823,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
807823
def update(self, __m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
808824
@overload
809825
def update(self, **kwargs: _VT) -> None: ...
810-
def keys(self) -> KeysView[_KT]: ...
811-
def values(self) -> ValuesView[_VT]: ...
812-
def items(self) -> ItemsView[_KT, _VT]: ...
826+
def keys(self) -> _dict_keys[_KT, _VT]: ...
827+
def values(self) -> _dict_values[_VT, _KT]: ...
828+
def items(self) -> _dict_items[_KT, _VT]: ...
813829
@classmethod
814830
@overload
815831
def fromkeys(cls, __iterable: Iterable[_T], __value: None = ...) -> dict[_T, Any | None]: ...

stdlib/collections/__init__.pyi

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import sys
22
from _typeshed import Self
3+
from builtins import _dict_items, _dict_keys, _dict_values
34
from typing import Any, Dict, Generic, NoReturn, Tuple, Type, TypeVar, overload
45

56
if sys.version_info >= (3, 10):
6-
from typing import (
7-
Callable,
8-
ItemsView,
9-
Iterable,
10-
Iterator,
11-
KeysView,
12-
Mapping,
13-
MutableMapping,
14-
MutableSequence,
15-
Reversible,
16-
Sequence,
17-
ValuesView,
18-
)
7+
from typing import Callable, Iterable, Iterator, Mapping, MutableMapping, MutableSequence, Reversible, Sequence
198
else:
209
from _collections_abc import *
2110

2211
_S = TypeVar("_S")
2312
_T = TypeVar("_T")
2413
_KT = TypeVar("_KT")
2514
_VT = TypeVar("_VT")
15+
_KT_co = TypeVar("_KT_co", covariant=True)
16+
_VT_co = TypeVar("_VT_co", covariant=True)
2617

2718
# namedtuple is special-cased in the type checker; the initializer is ignored.
2819
if sys.version_info >= (3, 7):
@@ -247,23 +238,25 @@ class Counter(Dict[_T, int], Generic[_T]):
247238
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
248239
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... # type: ignore
249240

250-
class _OrderedDictKeysView(KeysView[_KT], Reversible[_KT]):
251-
def __reversed__(self) -> Iterator[_KT]: ...
241+
class _OrderedDictKeysView(_dict_keys[_KT_co, _VT_co], Reversible[_KT_co]):
242+
def __reversed__(self) -> Iterator[_KT_co]: ...
252243

253-
class _OrderedDictItemsView(ItemsView[_KT, _VT], Reversible[Tuple[_KT, _VT]]):
254-
def __reversed__(self) -> Iterator[Tuple[_KT, _VT]]: ...
244+
class _OrderedDictItemsView(_dict_items[_KT_co, _VT_co], Reversible[Tuple[_KT_co, _VT_co]]):
245+
def __reversed__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
255246

256-
class _OrderedDictValuesView(ValuesView[_VT], Reversible[_VT]):
257-
def __reversed__(self) -> Iterator[_VT]: ...
247+
# The generics are the wrong way around because of a mypy limitation
248+
# https://github.com/python/mypy/issues/11138
249+
class _OrderedDictValuesView(_dict_values[_VT_co, _KT_co], Reversible[_VT_co], Generic[_VT_co, _KT_co]):
250+
def __reversed__(self) -> Iterator[_VT_co]: ...
258251

259252
class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
260253
def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ...
261254
def move_to_end(self, key: _KT, last: bool = ...) -> None: ...
262255
def copy(self: _S) -> _S: ...
263256
def __reversed__(self) -> Iterator[_KT]: ...
264-
def keys(self) -> _OrderedDictKeysView[_KT]: ...
257+
def keys(self) -> _OrderedDictKeysView[_KT, _VT]: ...
265258
def items(self) -> _OrderedDictItemsView[_KT, _VT]: ...
266-
def values(self) -> _OrderedDictValuesView[_VT]: ...
259+
def values(self) -> _OrderedDictValuesView[_VT, _KT]: ...
267260

268261
class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
269262
default_factory: Callable[[], _VT] | None

0 commit comments

Comments
 (0)