From 188cea2e5fb79b9f5516a0c0375beb8556198468 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 14:31:58 +0000 Subject: [PATCH 1/7] Bring `_collections_abc` closer to runtime definition --- stdlib/_collections_abc.pyi | 31 +++++++++++++++++++++++++++++++ stdlib/builtins.pyi | 21 +++++---------------- stdlib/collections/__init__.pyi | 9 +++++---- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 27d5234432f3..a23efb3f8e6c 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -1,3 +1,8 @@ +import sys +from types import EllipsisType as EllipsisType # undocumented +from types import FunctionType as FunctionType # undocumented +from types import GenericAlias as GenericAlias # undocumented +from types import MappingProxyType from typing import ( AbstractSet as Set, AsyncGenerator as AsyncGenerator, @@ -10,6 +15,7 @@ from typing import ( Container as Container, Coroutine as Coroutine, Generator as Generator, + Generic, Hashable as Hashable, ItemsView as ItemsView, Iterable as Iterable, @@ -23,8 +29,10 @@ from typing import ( Reversible as Reversible, Sequence as Sequence, Sized as Sized, + TypeVar, ValuesView as ValuesView, ) +from typing_extensions import final __all__ = [ "Awaitable", @@ -53,3 +61,26 @@ __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] + +mappingproxy = MappingProxyType # undocumented +generator = Generator # undocumented +coroutine = Coroutine # undocumented +async_generator = AsyncGenerator # undocumented diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 4ccaf52d4c8c..bca6e603f6c9 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -20,7 +20,7 @@ from _typeshed import ( SupportsWrite, ) from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper -from types import CodeType, MappingProxyType, TracebackType +from types import CodeType, TracebackType from typing import ( IO, AbstractSet, @@ -62,6 +62,7 @@ from typing import ( from typing_extensions import Literal, SupportsIndex, TypeGuard, final from _ast import AST +from _collections_abc import dict_items, dict_keys, dict_values if sys.version_info >= (3, 9): from types import GenericAlias @@ -809,18 +810,6 @@ class list(MutableSequence[_T], Generic[_T]): if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... -class _dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] - -class _dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] - -class _dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): - if sys.version_info >= (3, 10): - mapping: MappingProxyType[_KT_co, _VT_co] - class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def __init__(self: dict[_KT, _VT]) -> None: ... @@ -845,9 +834,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def update(self, __m: Iterable[tuple[_KT, _VT]], **kwargs: _VT) -> None: ... @overload def update(self, **kwargs: _VT) -> None: ... - def keys(self) -> _dict_keys[_KT, _VT]: ... - def values(self) -> _dict_values[_KT, _VT]: ... - def items(self) -> _dict_items[_KT, _VT]: ... + def keys(self) -> dict_keys[_KT, _VT]: ... + def values(self) -> dict_values[_KT, _VT]: ... + def items(self) -> dict_items[_KT, _VT]: ... @classmethod @overload def fromkeys(cls, __iterable: Iterable[_T], __value: None = ...) -> dict[_T, Any | None]: ... diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 0dad79cb1a4d..17ff45f5a3eb 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,9 +1,10 @@ import sys from _typeshed import Self -from builtins import _dict_items, _dict_keys, _dict_values from typing import Any, Dict, Generic, NoReturn, Tuple, Type, TypeVar, overload from typing_extensions import final +from _collections_abc import dict_items, dict_keys, dict_values + if sys.version_info >= (3, 10): from typing import Callable, Iterable, Iterator, Mapping, MutableMapping, MutableSequence, Reversible, Sequence else: @@ -242,15 +243,15 @@ class Counter(Dict[_T, int], Generic[_T]): def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... # type: ignore @final -class _OrderedDictKeysView(_dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): +class _OrderedDictKeysView(dict_keys[_KT_co, _VT_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]]): +class _OrderedDictItemsView(dict_items[_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]): +class _OrderedDictValuesView(dict_values[_KT_co, _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]): From f08a1f94f40f717bb86537c8bc8a13a87baf3389 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 14:44:40 +0000 Subject: [PATCH 2/7] Fix CI --- stdlib/_collections_abc.pyi | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index a23efb3f8e6c..2b77cdfe77db 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -1,8 +1,5 @@ import sys -from types import EllipsisType as EllipsisType # undocumented -from types import FunctionType as FunctionType # undocumented -from types import GenericAlias as GenericAlias # undocumented -from types import MappingProxyType +from types import FunctionType as FunctionType, GenericAlias as GenericAlias, MappingProxyType as mappingproxy # undocumented from typing import ( AbstractSet as Set, AsyncGenerator as AsyncGenerator, @@ -68,19 +65,19 @@ _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] + mapping: mappingproxy[_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] + mapping: mappingproxy[_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] + mapping: mappingproxy[_KT_co, _VT_co] -mappingproxy = MappingProxyType # undocumented +EllipsisType = ellipsis # undocumented # noqa F811 from builtins generator = Generator # undocumented coroutine = Coroutine # undocumented async_generator = AsyncGenerator # undocumented From a439d4d5900aea357e31eec44bd51777f84db038 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 14:46:35 +0000 Subject: [PATCH 3/7] Remove unused imports --- stdlib/builtins.pyi | 3 --- 1 file changed, 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index bca6e603f6c9..4686fdde5fb0 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -32,10 +32,8 @@ from typing import ( Callable, FrozenSet, Generic, - ItemsView, Iterable, Iterator, - KeysView, Mapping, MutableMapping, MutableSequence, @@ -56,7 +54,6 @@ from typing import ( Type, TypeVar, Union, - ValuesView, overload, ) from typing_extensions import Literal, SupportsIndex, TypeGuard, final From 1875503c74d866c7991e2dc34beb3f5c9dbe19f6 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 14:49:20 +0000 Subject: [PATCH 4/7] This is what I get for running 3.10 --- stdlib/_collections_abc.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 2b77cdfe77db..60dc238ae5d3 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -1,5 +1,5 @@ import sys -from types import FunctionType as FunctionType, GenericAlias as GenericAlias, MappingProxyType as mappingproxy # undocumented +from types import FunctionType as FunctionType, MappingProxyType as mappingproxy # undocumented from typing import ( AbstractSet as Set, AsyncGenerator as AsyncGenerator, @@ -31,6 +31,9 @@ from typing import ( ) from typing_extensions import final +if sys.version_info >= (3, 9): + from types import GenericAlias as GenericAlias # undocumented + __all__ = [ "Awaitable", "Coroutine", From 0655befc977e2de80b44ff09787adf7f12a19401 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 15:08:52 +0000 Subject: [PATCH 5/7] Tweak --- stdlib/_collections_abc.pyi | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 60dc238ae5d3..0e4b63c9dd05 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -2,6 +2,7 @@ import sys from types import FunctionType as FunctionType, MappingProxyType as mappingproxy # undocumented from typing import ( AbstractSet as Set, + Any, AsyncGenerator as AsyncGenerator, AsyncIterable as AsyncIterable, AsyncIterator as AsyncIterator, @@ -26,6 +27,7 @@ from typing import ( Reversible as Reversible, Sequence as Sequence, Sized as Sized, + Type, TypeVar, ValuesView as ValuesView, ) @@ -81,6 +83,6 @@ class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocum mapping: mappingproxy[_KT_co, _VT_co] EllipsisType = ellipsis # undocumented # noqa F811 from builtins -generator = Generator # undocumented -coroutine = Coroutine # undocumented -async_generator = AsyncGenerator # undocumented +generator: Type[Generator[Any, Any, Any]] = ... # undocumented +coroutine: Type[Coroutine[Any, Any, Any]] = ... # undocumented +async_generator: Type[AsyncGenerator[Any, Any]] = ... # undocumented From d7191bafe612224a9d1d35f6002156460ea6b35d Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 15:14:42 +0000 Subject: [PATCH 6/7] Remove unneeded `TypeVar`s from builtins --- stdlib/builtins.pyi | 2 -- 1 file changed, 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 4686fdde5fb0..0a8249af8c47 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -72,8 +72,6 @@ _T_co = TypeVar("_T_co", covariant=True) _T_contra = TypeVar("_T_contra", contravariant=True) _KT = TypeVar("_KT") _VT = TypeVar("_VT") -_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers. -_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers. _S = TypeVar("_S") _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") From 177e4c36ccbb9a2e2ed5d080017b7171ae5fac46 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 Nov 2021 17:14:45 +0000 Subject: [PATCH 7/7] Remove incidental imports --- stdlib/_collections_abc.pyi | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 0e4b63c9dd05..223b5fb8d62e 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -1,8 +1,7 @@ import sys -from types import FunctionType as FunctionType, MappingProxyType as mappingproxy # undocumented +from types import MappingProxyType from typing import ( AbstractSet as Set, - Any, AsyncGenerator as AsyncGenerator, AsyncIterable as AsyncIterable, AsyncIterator as AsyncIterator, @@ -27,15 +26,11 @@ from typing import ( Reversible as Reversible, Sequence as Sequence, Sized as Sized, - Type, TypeVar, ValuesView as ValuesView, ) from typing_extensions import final -if sys.version_info >= (3, 9): - from types import GenericAlias as GenericAlias # undocumented - __all__ = [ "Awaitable", "Coroutine", @@ -70,19 +65,14 @@ _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: mappingproxy[_KT_co, _VT_co] + 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: mappingproxy[_KT_co, _VT_co] + 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: mappingproxy[_KT_co, _VT_co] - -EllipsisType = ellipsis # undocumented # noqa F811 from builtins -generator: Type[Generator[Any, Any, Any]] = ... # undocumented -coroutine: Type[Coroutine[Any, Any, Any]] = ... # undocumented -async_generator: Type[AsyncGenerator[Any, Any]] = ... # undocumented + mapping: MappingProxyType[_KT_co, _VT_co]