From cf0b3a259665b7595a91a12472c92b0eb8a6d2ca Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 13 Aug 2022 05:02:43 +0200 Subject: [PATCH] Improve `.keys()`, `.values()`, `.items()` methods for `TypedDict`s (#8532) --- stdlib/typing.pyi | 7 ++++--- stdlib/typing_extensions.pyi | 10 ++++------ stubs/mypy-extensions/mypy_extensions.pyi | 9 +++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index a186bb92b..5afd72ae7 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -1,6 +1,7 @@ import _typeshed import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys +from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import IdentityFunction, Incomplete, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod from contextlib import AbstractAsyncContextManager, AbstractContextManager @@ -797,9 +798,9 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta): def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse] def update(self: _T, __m: _T) -> None: ... def __delitem__(self, k: NoReturn) -> None: ... - def items(self) -> ItemsView[str, object]: ... - def keys(self) -> KeysView[str]: ... - def values(self) -> ValuesView[object]: ... + def items(self) -> dict_items[str, object]: ... + def keys(self) -> dict_keys[str, object]: ... + def values(self) -> dict_values[str, object]: ... if sys.version_info >= (3, 9): def __or__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ... def __ior__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ... diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index edc0d228e..dbc89f221 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -2,6 +2,7 @@ import _typeshed import abc import collections import sys +from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import IdentityFunction from collections.abc import Iterable from typing import ( # noqa: Y022,Y027,Y039 @@ -20,8 +21,6 @@ from typing import ( # noqa: Y022,Y027,Y039 Counter as Counter, DefaultDict as DefaultDict, Deque as Deque, - ItemsView, - KeysView, Mapping, NewType as NewType, NoReturn as NoReturn, @@ -29,7 +28,6 @@ from typing import ( # noqa: Y022,Y027,Y039 Text as Text, Type as Type, TypeVar, - ValuesView, _Alias, overload as overload, type_check_only, @@ -135,9 +133,9 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): # Mypy plugin hook for 'pop' expects that 'default' has a type variable type. def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse] def update(self: _T, __m: _T) -> None: ... - def items(self) -> ItemsView[str, object]: ... - def keys(self) -> KeysView[str]: ... - def values(self) -> ValuesView[object]: ... + def items(self) -> dict_items[str, object]: ... + def keys(self) -> dict_keys[str, object]: ... + def values(self) -> dict_values[str, object]: ... def __delitem__(self, k: NoReturn) -> None: ... if sys.version_info >= (3, 9): def __or__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ... diff --git a/stubs/mypy-extensions/mypy_extensions.pyi b/stubs/mypy-extensions/mypy_extensions.pyi index edefcc318..a7be3f36a 100644 --- a/stubs/mypy-extensions/mypy_extensions.pyi +++ b/stubs/mypy-extensions/mypy_extensions.pyi @@ -1,7 +1,8 @@ import abc import sys +from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import IdentityFunction, Self -from collections.abc import ItemsView, KeysView, Mapping, ValuesView +from collections.abc import Mapping from typing import Any, ClassVar, Generic, TypeVar, overload, type_check_only _T = TypeVar("_T") @@ -21,9 +22,9 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): # Mypy plugin hook for 'pop' expects that 'default' has a type variable type. def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse] def update(self: Self, __m: Self) -> None: ... - def items(self) -> ItemsView[str, object]: ... - def keys(self) -> KeysView[str]: ... - def values(self) -> ValuesView[object]: ... + def items(self) -> dict_items[str, object]: ... + def keys(self) -> dict_keys[str, object]: ... + def values(self) -> dict_values[str, object]: ... def __delitem__(self, k: NoReturn) -> None: ... if sys.version_info >= (3, 9): def __or__(self: Self, __other: Self) -> Self: ...