diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 223b5fb8d62e..27d5234432f3 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -1,5 +1,3 @@ -import sys -from types import MappingProxyType from typing import ( AbstractSet as Set, AsyncGenerator as AsyncGenerator, @@ -12,7 +10,6 @@ from typing import ( Container as Container, Coroutine as Coroutine, Generator as Generator, - Generic, Hashable as Hashable, ItemsView as ItemsView, Iterable as Iterable, @@ -26,10 +23,8 @@ from typing import ( Reversible as Reversible, Sequence as Sequence, Sized as Sized, - TypeVar, ValuesView as ValuesView, ) -from typing_extensions import final __all__ = [ "Awaitable", @@ -58,21 +53,3 @@ __all__ = [ "MutableSequence", "ByteString", ] - -_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers. -_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers. - -@final -class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] - -@final -class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] - -@final -class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocumented - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 1d6446b59070..e807fffee2b7 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1,7 +1,6 @@ import sys import types from _ast import AST -from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( OpenBinaryMode, OpenBinaryModeReading, @@ -32,8 +31,10 @@ from typing import ( ByteString, Callable, Generic, + ItemsView, Iterable, Iterator, + KeysView, Mapping, MutableMapping, MutableSequence, @@ -51,6 +52,7 @@ from typing import ( SupportsRound, TypeVar, Union, + ValuesView, overload, ) from typing_extensions import Literal, SupportsIndex, TypeGuard, final @@ -847,9 +849,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None: ... def __new__(cls: type[Self], *args: Any, **kwargs: Any) -> Self: ... def copy(self) -> dict[_KT, _VT]: ... - def keys(self) -> dict_keys[_KT, _VT]: ... - def values(self) -> dict_values[_KT, _VT]: ... - def items(self) -> dict_items[_KT, _VT]: ... + def keys(self) -> KeysView[_KT]: ... + def values(self) -> ValuesView[_VT]: ... + def items(self) -> ItemsView[_KT, _VT]: ... # Signature of `dict.fromkeys` should be kept identical to `fromkeys` methods of `OrderedDict`/`ChainMap`/`UserDict` in `collections` # TODO: the true signature of `dict.fromkeys` is not expressible in the current type system. # See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963. diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index f1e14a4ce10b..0a853de45f44 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,5 +1,4 @@ import sys -from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT from typing import Any, Generic, NoReturn, TypeVar, overload from typing_extensions import SupportsIndex, final @@ -256,15 +255,15 @@ class Counter(dict[_T, int], Generic[_T]): def total(self) -> int: ... @final -class _OrderedDictKeysView(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] +class _OrderedDictKeysView(KeysView[_KT_co], Reversible[_KT_co]): # type: ignore[misc] def __reversed__(self) -> Iterator[_KT_co]: ... @final -class _OrderedDictItemsView(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] +class _OrderedDictItemsView(ItemsView[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ... @final -class _OrderedDictValuesView(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] +class _OrderedDictValuesView(ValuesView[_VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] def __reversed__(self) -> Iterator[_VT_co]: ... class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): @@ -272,7 +271,7 @@ class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def move_to_end(self, key: _KT, last: bool = ...) -> None: ... def copy(self: Self) -> Self: ... def __reversed__(self) -> Iterator[_KT]: ... - def keys(self) -> _OrderedDictKeysView[_KT, _VT]: ... + def keys(self) -> _OrderedDictKeysView[_KT]: ... def items(self) -> _OrderedDictItemsView[_KT, _VT]: ... def values(self) -> _OrderedDictValuesView[_KT, _VT]: ... # `fromkeys` is actually inherited from `dict` at runtime, so the signature should be kept in line with `dict.fromkeys`. diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 1ec9ba720545..adb6aaff52e0 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -2,7 +2,7 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys from _typeshed import Self, SupportsKeysAndGetItem from abc import ABCMeta, abstractmethod -from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType +from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MappingProxyType, MethodType, ModuleType, TracebackType from typing_extensions import Literal as _Literal, ParamSpec as _ParamSpec, final as _final if sys.version_info >= (3, 7): @@ -414,6 +414,8 @@ class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co, def __rsub__(self, o: Iterable[_T]) -> set[_T]: ... def __xor__(self, o: Iterable[_T]) -> set[tuple[_KT_co, _VT_co] | _T]: ... def __rxor__(self, o: Iterable[_T]) -> set[tuple[_KT_co, _VT_co] | _T]: ... + if sys.version_info >= (3, 10): + mapping: MappingProxyType[_KT_co, _VT_co] class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]): def __init__(self, mapping: Mapping[_KT_co, Any]) -> None: ... # undocumented @@ -429,6 +431,8 @@ class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]): def __rsub__(self, o: Iterable[_T]) -> set[_T]: ... def __xor__(self, o: Iterable[_T]) -> set[_KT_co | _T]: ... def __rxor__(self, o: Iterable[_T]) -> set[_KT_co | _T]: ... + if sys.version_info >= (3, 10): + mapping: MappingProxyType[_KT_co, _VT_co] class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]): def __init__(self, mapping: Mapping[Any, _VT_co]) -> None: ... # undocumented @@ -436,6 +440,8 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]): def __iter__(self) -> Iterator[_VT_co]: ... if sys.version_info >= (3, 8): def __reversed__(self) -> Iterator[_VT_co]: ... + if sys.version_info >= (3, 10): + mapping: MappingProxyType[_KT_co, _VT_co] @runtime_checkable class ContextManager(Protocol[_T_co]):