Skip to content

Commit cf0b3a2

Browse files
authored
Improve .keys(), .values(), .items() methods for TypedDicts (#8532)
1 parent 2efaa6a commit cf0b3a2

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

stdlib/typing.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _typeshed
22
import collections # Needed by aliases like DefaultDict, see mypy issue 2986
33
import sys
4+
from _collections_abc import dict_items, dict_keys, dict_values
45
from _typeshed import IdentityFunction, Incomplete, SupportsKeysAndGetItem
56
from abc import ABCMeta, abstractmethod
67
from contextlib import AbstractAsyncContextManager, AbstractContextManager
@@ -797,9 +798,9 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
797798
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
798799
def update(self: _T, __m: _T) -> None: ...
799800
def __delitem__(self, k: NoReturn) -> None: ...
800-
def items(self) -> ItemsView[str, object]: ...
801-
def keys(self) -> KeysView[str]: ...
802-
def values(self) -> ValuesView[object]: ...
801+
def items(self) -> dict_items[str, object]: ...
802+
def keys(self) -> dict_keys[str, object]: ...
803+
def values(self) -> dict_values[str, object]: ...
803804
if sys.version_info >= (3, 9):
804805
def __or__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ...
805806
def __ior__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ...

stdlib/typing_extensions.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _typeshed
22
import abc
33
import collections
44
import sys
5+
from _collections_abc import dict_items, dict_keys, dict_values
56
from _typeshed import IdentityFunction
67
from collections.abc import Iterable
78
from typing import ( # noqa: Y022,Y027,Y039
@@ -20,16 +21,13 @@ from typing import ( # noqa: Y022,Y027,Y039
2021
Counter as Counter,
2122
DefaultDict as DefaultDict,
2223
Deque as Deque,
23-
ItemsView,
24-
KeysView,
2524
Mapping,
2625
NewType as NewType,
2726
NoReturn as NoReturn,
2827
Sequence,
2928
Text as Text,
3029
Type as Type,
3130
TypeVar,
32-
ValuesView,
3331
_Alias,
3432
overload as overload,
3533
type_check_only,
@@ -135,9 +133,9 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
135133
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
136134
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
137135
def update(self: _T, __m: _T) -> None: ...
138-
def items(self) -> ItemsView[str, object]: ...
139-
def keys(self) -> KeysView[str]: ...
140-
def values(self) -> ValuesView[object]: ...
136+
def items(self) -> dict_items[str, object]: ...
137+
def keys(self) -> dict_keys[str, object]: ...
138+
def values(self) -> dict_values[str, object]: ...
141139
def __delitem__(self, k: NoReturn) -> None: ...
142140
if sys.version_info >= (3, 9):
143141
def __or__(self: _typeshed.Self, __value: _typeshed.Self) -> _typeshed.Self: ...

stubs/mypy-extensions/mypy_extensions.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import abc
22
import sys
3+
from _collections_abc import dict_items, dict_keys, dict_values
34
from _typeshed import IdentityFunction, Self
4-
from collections.abc import ItemsView, KeysView, Mapping, ValuesView
5+
from collections.abc import Mapping
56
from typing import Any, ClassVar, Generic, TypeVar, overload, type_check_only
67

78
_T = TypeVar("_T")
@@ -21,9 +22,9 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta):
2122
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
2223
def pop(self, k: NoReturn, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
2324
def update(self: Self, __m: Self) -> None: ...
24-
def items(self) -> ItemsView[str, object]: ...
25-
def keys(self) -> KeysView[str]: ...
26-
def values(self) -> ValuesView[object]: ...
25+
def items(self) -> dict_items[str, object]: ...
26+
def keys(self) -> dict_keys[str, object]: ...
27+
def values(self) -> dict_values[str, object]: ...
2728
def __delitem__(self, k: NoReturn) -> None: ...
2829
if sys.version_info >= (3, 9):
2930
def __or__(self: Self, __other: Self) -> Self: ...

0 commit comments

Comments
 (0)