8000 add dict.{keys,values,items}().mapping · python/typeshed@0cf1e25 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cf1e25

Browse files
committed
add dict.{keys,values,items}().mapping
1 parent e4c2c0f commit 0cf1e25

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
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]: ...

0 commit comments

Comments
 (0)
0