From 985e6e776332b3e6580055a12b66b438c01c891d Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 30 Jul 2021 14:12:55 +0200 Subject: [PATCH 1/4] Annotate three previously missing `MappingProxyType` methods * `__hash__` * `__reversed__` * `__class_getitem__` --- stdlib/types.pyi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index fbd9e0645816..39ba38e76ecd 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -138,11 +138,16 @@ class CodeType: ) -> CodeType: ... class MappingProxyType(Mapping[_KT, _VT], Generic[_KT, _VT]): + __hash__: None # type: ignore def __init__(self, mapping: Mapping[_KT, _VT]) -> None: ... def __getitem__(self, k: _KT) -> _VT: ... def __iter__(self) -> Iterator[_KT]: ... def __len__(self) -> int: ... def copy(self) -> Dict[_KT, _VT]: ... + if sys.version_info >= (3, 8): + def __reversed__(self) -> Iterator[_KT]: ... + if sys.version_info >= (3, 9): + def __class_getitem__(cls, item: Any) -> GenericAlias: ... class SimpleNamespace: def __init__(self, **kwargs: Any) -> None: ... From 54b4b1cd8c81cc7a760d44833726ae73b8341e6f Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 30 Jul 2021 14:13:52 +0200 Subject: [PATCH 2/4] Improve 5 `MappingProxyType` methods The assumption here is that the underlying mapping is a `dict`, just is done for `MappingProxyType.copy` --- stdlib/types.pyi | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 39ba38e76ecd..83038bab5dff 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -10,13 +10,16 @@ from typing import ( Dict, Generator, Generic, + ItemsView, Iterable, Iterator, + KeysView, Mapping, Optional, Tuple, Type, TypeVar, + ValuesView, overload, ) from typing_extensions import Literal, final @@ -24,6 +27,8 @@ from typing_extensions import Literal, final # Note, all classes "defined" here require special handling. _T = TypeVar("_T") +_T1 = TypeVar("_T1") +_T2 = TypeVar("_T2") _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _KT = TypeVar("_KT") @@ -144,10 +149,15 @@ class MappingProxyType(Mapping[_KT, _VT], Generic[_KT, _VT]): def __iter__(self) -> Iterator[_KT]: ... def __len__(self) -> int: ... def copy(self) -> Dict[_KT, _VT]: ... + def keys(self) -> KeysView[_KT]: ... + def values(self) -> ValuesView[_VT]: ... + def items(self) -> ItemsView[_KT, _VT]: ... if sys.version_info >= (3, 8): def __reversed__(self) -> Iterator[_KT]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... + def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... + def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... class SimpleNamespace: def __init__(self, **kwargs: Any) -> None: ... From 8b7fac634113d67b3b33edfc9dc53026d7113269 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 30 Jul 2021 14:41:17 +0200 Subject: [PATCH 3/4] Move `__reversed__` to python >= 3.9 --- stdlib/types.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 83038bab5dff..5d7985dd33d1 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -152,10 +152,9 @@ class MappingProxyType(Mapping[_KT, _VT], Generic[_KT, _VT]): def keys(self) -> KeysView[_KT]: ... def values(self) -> ValuesView[_VT]: ... def items(self) -> ItemsView[_KT, _VT]: ... - if sys.version_info >= (3, 8): - def __reversed__(self) -> Iterator[_KT]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... + def __reversed__(self) -> Iterator[_KT]: ... def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... From 4a864e98e84c0799c7f39a744d7763a8a7208ecf Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 30 Jul 2021 16:15:05 +0200 Subject: [PATCH 4/4] Make the value type of `types.MappingProxyType` covariant Co-Authored-By: Sebastian Rittau --- stdlib/types.pyi | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 5d7985dd33d1..a1403fea637b 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -32,7 +32,7 @@ _T2 = TypeVar("_T2") _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _KT = TypeVar("_KT") -_VT = TypeVar("_VT") +_VT_co = TypeVar("_VT_co", covariant=True) _V_co = TypeVar("_V_co", covariant=True) class _Cell: @@ -142,21 +142,21 @@ class CodeType: co_lnotab: bytes = ..., ) -> CodeType: ... -class MappingProxyType(Mapping[_KT, _VT], Generic[_KT, _VT]): +class MappingProxyType(Mapping[_KT, _VT_co], Generic[_KT, _VT_co]): __hash__: None # type: ignore - def __init__(self, mapping: Mapping[_KT, _VT]) -> None: ... - def __getitem__(self, k: _KT) -> _VT: ... + def __init__(self, mapping: Mapping[_KT, _VT_co]) -> None: ... + def __getitem__(self, k: _KT) -> _VT_co: ... def __iter__(self) -> Iterator[_KT]: ... def __len__(self) -> int: ... - def copy(self) -> Dict[_KT, _VT]: ... + def copy(self) -> Dict[_KT, _VT_co]: ... def keys(self) -> KeysView[_KT]: ... - def values(self) -> ValuesView[_VT]: ... - def items(self) -> ItemsView[_KT, _VT]: ... + def values(self) -> ValuesView[_VT_co]: ... + def items(self) -> ItemsView[_KT, _VT_co]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... def __reversed__(self) -> Iterator[_KT]: ... - def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... - def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ... + def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT_co | _T2]: ... + def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT_co | _T2]: ... class SimpleNamespace: def __init__(self, **kwargs: Any) -> None: ...