From 78a77de5c37de32c477683abac0c4ba62a637b59 Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Sun, 1 Oct 2023 19:58:37 +0100 Subject: [PATCH 01/11] Make more consistent with runtime implementation --- stdlib/types.pyi | 23 ++++++++++++++++++++++- stdlib/typing.pyi | 34 +++------------------------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 8559063834c9..8a348e79f96d 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -1,5 +1,4 @@ import sys -from _typeshed import SupportsKeysAndGetItem from collections.abc import ( AsyncGenerator, Awaitable, @@ -17,6 +16,8 @@ from importlib.machinery import ModuleSpec # pytype crashes if types.MappingProxyType inherits from collections.abc.Mapping instead of typing.Mapping from typing import Any, ClassVar, Generic, Mapping, Protocol, TypeVar, overload # noqa: Y022 + +from _typeshed import SupportsKeysAndGetItem from typing_extensions import Literal, ParamSpec, Self, TypeVarTuple, final __all__ = [ @@ -358,6 +359,12 @@ _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) @final class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]): + @property + def gi_code(self) -> CodeType: ... + @property + def gi_frame(self) -> FrameType: ... + @property + def gi_running(self) -> bool: ... @property def gi_yieldfrom(self) -> GeneratorType[_YieldT_co, _SendT_contra, Any] | None: ... if sys.version_info >= (3, 11): @@ -379,6 +386,12 @@ class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]): class AsyncGeneratorType(AsyncGenerator[_YieldT_co, _SendT_contra]): @property def ag_await(self) -> Awaitable[Any] | None: ... + @property + def ag_code(self) -> CodeType: ... + @property + def ag_frame(self) -> FrameType: ... + @property + def ag_running(self) -> bool: ... __name__: str __qualname__: str if sys.version_info >= (3, 12): @@ -403,6 +416,14 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]): __name__: str __qualname__: str @property + def cr_await(self) -> Any | None: ... + @property + def cr_code(self) -> CodeType: ... + @property + def cr_frame(self) -> FrameType: ... + @property + def cr_running(self) -> bool: ... + @property def cr_origin(self) -> tuple[tuple[str, int, str], ...] | None: ... if sys.version_info >= (3, 11): @property diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 6deb0ffd02b3..6a1712a8650d 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -406,8 +406,7 @@ _YieldT_co = TypeVar("_YieldT_co", covariant=True) _SendT_contra = TypeVar("_SendT_contra", contravariant=True) _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) -class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]): - def __next__(self) -> _YieldT_co: ... +class Generator(Iterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]): @abstractmethod def send(self, __value: _SendT_contra) -> _YieldT_co: ... @overload @@ -419,32 +418,15 @@ class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _Return @abstractmethod def throw(self, __typ: BaseException, __val: None = None, __tb: TracebackType | None = None) -> _YieldT_co: ... def close(self) -> None: ... - def __iter__(self) -> Generator[_YieldT_co, _SendT_contra, _ReturnT_co]: ... - @property - def gi_code(self) -> CodeType: ... - @property - def gi_frame(self) -> FrameType: ... - @property - def gi_running(self) -> bool: ... - @property - def gi_yieldfrom(self) -> Generator[Any, Any, Any] | None: ... @runtime_checkable class Awaitable(Protocol[_T_co]): @abstractmethod def __await__(self) -> Generator[Any, None, _T_co]: ... -class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]): +class Coroutine(Awaitable[_ReturnT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]): __name__: str __qualname__: str - @property - def cr_await(self) -> Any | None: ... - @property - def cr_code(self) -> CodeType: ... - @property - def cr_frame(self) -> FrameType: ... - @property - def cr_running(self) -> bool: ... @abstractmethod def send(self, __value: _SendT_contra) -> _YieldT_co: ... @overload @@ -455,7 +437,6 @@ class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _Retu @overload @abstractmethod def throw(self, __typ: BaseException, __val: None = None, __tb: TracebackType | None = None) -> _YieldT_co: ... - @abstractmethod def close(self) -> None: ... # NOTE: This type does not exist in typing.py or PEP 484 but mypy needs it to exist. @@ -479,8 +460,7 @@ class AsyncIterator(AsyncIterable[_T_co], Protocol[_T_co]): def __anext__(self) -> Awaitable[_T_co]: ... def __aiter__(self) -> AsyncIterator[_T_co]: ... -class AsyncGenerator(AsyncIterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra]): - def __anext__(self) -> Awaitable[_YieldT_co]: ... +class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra]): @abstractmethod def asend(self, __value: _SendT_contra) -> Awaitable[_YieldT_co]: ... @overload @@ -492,14 +472,6 @@ class AsyncGenerator(AsyncIterator[_YieldT_co], Generic[_YieldT_co, _SendT_contr @abstractmethod def athrow(self, __typ: BaseException, __val: None = None, __tb: TracebackType | None = None) -> Awaitable[_YieldT_co]: ... def aclose(self) -> Awaitable[None]: ... - @property - def ag_await(self) -> Any: ... - @property - def ag_code(self) -> CodeType: ... - @property - def ag_frame(self) -> FrameType: ... - @property - def ag_running(self) -> bool: ... @runtime_checkable class Container(Protocol[_T_co]): From 0fd644feed0d61da11c98a429a8d636a098ac8ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 19:00:06 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/types.pyi | 3 +-- stdlib/typing.pyi | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 8a348e79f96d..ff1463023d5a 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import SupportsKeysAndGetItem from collections.abc import ( AsyncGenerator, Awaitable, @@ -16,8 +17,6 @@ from importlib.machinery import ModuleSpec # pytype crashes if types.MappingProxyType inherits from collections.abc.Mapping instead of typing.Mapping from typing import Any, ClassVar, Generic, Mapping, Protocol, TypeVar, overload # noqa: Y022 - -from _typeshed import SupportsKeysAndGetItem from typing_extensions import Literal, ParamSpec, Self, TypeVarTuple, final __all__ = [ diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 6a1712a8650d..63b58fac3224 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -9,7 +9,6 @@ from re import Match as Match, Pattern as Pattern from types import ( BuiltinFunctionType, CodeType, - FrameType, FunctionType, MethodDescriptorType, MethodType, From 049a270128fd7e2ca1562f0ba61451b8e2ee4381 Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Sun, 1 Oct 2023 20:17:26 +0100 Subject: [PATCH 03/11] mark as runtime checkable --- stdlib/typing.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 63b58fac3224..bced0a025456 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -405,6 +405,7 @@ _YieldT_co = TypeVar("_YieldT_co", covariant=True) _SendT_contra = TypeVar("_SendT_contra", contravariant=True) _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) +@runtime_checkable class Generator(Iterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]): @abstractmethod def send(self, __value: _SendT_contra) -> _YieldT_co: ... @@ -423,6 +424,7 @@ class Awaitable(Protocol[_T_co]): @abstractmethod def __await__(self) -> Generator[Any, None, _T_co]: ... +@runtime_checkable class Coroutine(Awaitable[_ReturnT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]): __name__: str __qualname__: str @@ -459,6 +461,7 @@ class AsyncIterator(AsyncIterable[_T_co], Protocol[_T_co]): def __anext__(self) -> Awaitable[_T_co]: ... def __aiter__(self) -> AsyncIterator[_T_co]: ... +@runtime_checkable class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra]): @abstractmethod def asend(self, __value: _SendT_contra) -> Awaitable[_YieldT_co]: ... From ab4613bfe049e8d96f90788e2501e1ece03e3ac7 Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Mon, 2 Oct 2023 00:58:51 +0100 Subject: [PATCH 04/11] This might help a smidge --- stdlib/typing.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index bced0a025456..b8fc4a7be91e 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -407,6 +407,7 @@ _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) @runtime_checkable class Generator(Iterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra, _ReturnT_co]): + def __next__(self) -> _YieldT_co: ... @abstractmethod def send(self, __value: _SendT_contra) -> _YieldT_co: ... @overload From c7fead5519d8a4bc7b9c596b770653a490a6c6c6 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 1 Oct 2024 18:21:05 -0700 Subject: [PATCH 05/11] Update stdlib/typing.pyi --- stdlib/typing.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 3b03400f7838..d4563c4ca85c 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -463,6 +463,7 @@ class Generator(Iterator[_YieldT_co], Protocol[_YieldT_co, _SendT_contra, _Retur @abstractmethod def throw(self, typ: BaseException, val: None = None, tb: TracebackType | None = None, /) -> _YieldT_co: ... def close(self) -> None: ... + def __iter__(self) -> Generator[_YieldT_co, _SendT_contra, _ReturnT_co]: ... # NOTE: Prior to Python 3.13 these aliases are lacking the second _ExitT_co parameter if sys.version_info >= (3, 13): From 92782a297eeaa80d09a42bd663b8c08f367af55a Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 2 Oct 2024 12:07:58 +0100 Subject: [PATCH 06/11] Update common.txt --- stdlib/@tests/stubtest_allowlists/common.txt | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 01b23cd7a274..0306d9a3b8ae 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -4,10 +4,6 @@ # Please keep sorted alphabetically -_collections_abc.AsyncGenerator.ag_await -_collections_abc.AsyncGenerator.ag_code -_collections_abc.AsyncGenerator.ag_frame -_collections_abc.AsyncGenerator.ag_running _collections_abc.ItemsView.__reversed__ _collections_abc.KeysView.__reversed__ _collections_abc.ValuesView.__reversed__ @@ -288,14 +284,6 @@ _collections_abc.AsyncGenerator.aclose _collections_abc.AsyncIterator.__anext__ # Coroutine and Generator properties are added programmatically -_collections_abc.Coroutine.cr_await -_collections_abc.Coroutine.cr_code -_collections_abc.Coroutine.cr_frame -_collections_abc.Coroutine.cr_running -_collections_abc.Generator.gi_code -_collections_abc.Generator.gi_frame -_collections_abc.Generator.gi_running -_collections_abc.Generator.gi_yieldfrom _collections_abc.Mapping.__reversed__ # set to None at runtime for a better error message _collections_abc.Mapping.get # Adding None to the Union messed up mypy _collections_abc.Sequence.index # Supporting None in end is not mandatory From cab10a8223acf09599ed3992acc26bb56c1ea6af Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 2 Oct 2024 12:21:52 +0100 Subject: [PATCH 07/11] Update py38.txt --- stdlib/@tests/stubtest_allowlists/py38.txt | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index e66b499714df..5e4c433e35ba 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -19,10 +19,6 @@ collections.AsyncGenerator.asend # async at runtime, deliberately not in the st collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491 collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 -collections.AsyncGenerator.ag_await -collections.AsyncGenerator.ag_code -collections.AsyncGenerator.ag_frame -collections.AsyncGenerator.ag_running collections.ByteString # see comments in py3_common.txt collections.Callable collections.ItemsView.__reversed__ @@ -90,14 +86,6 @@ typing_extensions\.TypeAliasType\.__init_subclass__ # We call them read-only properties, runtime implementation is slightly different typing_extensions\.TypeAliasType\.__(parameters|type_params|name|module|value)__ -collections.Coroutine.cr_await -collections.Coroutine.cr_code -collections.Coroutine.cr_frame -collections.Coroutine.cr_running -collections.Generator.gi_code -collections.Generator.gi_frame -collections.Generator.gi_running -collections.Generator.gi_yieldfrom collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory From 255b6f026706b3b19607aa8cc2e82e58e6258313 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 2 Oct 2024 12:31:44 +0100 Subject: [PATCH 08/11] Update py39.txt --- stdlib/@tests/stubtest_allowlists/py39.txt | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index e7217a040654..5fac4a158495 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -8,10 +8,6 @@ collections.AsyncGenerator.asend # async at runtime, deliberately not in the st collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491 collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 -collections.AsyncGenerator.ag_await -collections.AsyncGenerator.ag_code -collections.AsyncGenerator.ag_frame -collections.AsyncGenerator.ag_running collections.ByteString # see comments in py3_common.txt collections.Callable collections.Mapping.__reversed__ # Set to None at runtime for a better error message @@ -78,14 +74,6 @@ typing_extensions\.TypeAliasType\.__init_subclass__ # We call them read-only properties, runtime implementation is slightly different typing_extensions\.TypeAliasType\.__(parameters|type_params|name|module|value)__ -collections.Coroutine.cr_await -collections.Coroutine.cr_code -collections.Coroutine.cr_frame -collections.Coroutine.cr_running -collections.Generator.gi_code -collections.Generator.gi_frame -collections.Generator.gi_running -collections.Generator.gi_yieldfrom collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory From fae5bed70bc5a7b1c0b8b864622296379bbdbe17 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Dec 2024 21:02:40 -0800 Subject: [PATCH 09/11] fixup --- stdlib/@tests/stubtest_allowlists/py38.txt | 11 +++-------- stdlib/@tests/stubtest_allowlists/py39.txt | 10 +--------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 3c87658074ea..3724db362d2b 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -62,14 +62,6 @@ collections.AsyncGenerator.aclose # async at runtime, deliberately not in the s collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.ByteString # see comments in py3_common.txt collections.Callable -collections.Coroutine.cr_await -collections.Coroutine.cr_code -collections.Coroutine.cr_frame -collections.Coroutine.cr_running -collections.Generator.gi_code -collections.Generator.gi_frame -collections.Generator.gi_running -collections.Generator.gi_yieldfrom collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs @@ -150,6 +142,9 @@ tkinter.tix.TkVersion # Allowlist entries that cannot or should not be fixed; <= 3.8 # ============================================================ +asyncio.locks._ContextManagerMixin.__enter__ # Always raises; deliberately omitted from the stub +asyncio.locks._ContextManagerMixin.__exit__ # Always raises; deliberately omitted from the stub + # Incompatible changes introduced in Python 3.8.20 # (Remove once 3.8.20 becomes available for GitHub Actions) email._header_value_parser.NLSET diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 09c941067388..6448c7ede697 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -50,17 +50,10 @@ typing._SpecialForm.__mro_entries__ # Exists at runtime, but missing from stubs builtins.input # Incorrect default value in text signature, fixed in 3.10 collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491 +collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also. collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.ByteString # see comments in py3_common.txt collections.Callable -collections.Coroutine.cr_await -collections.Coroutine.cr_code -collections.Coroutine.cr_frame -collections.Coroutine.cr_running -collections.Generator.gi_code -collections.Generator.gi_frame -collections.Generator.gi_running -collections.Generator.gi_yieldfrom collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs @@ -136,7 +129,6 @@ tkinter.tix.Shell tkinter.tix.TclVersion tkinter.tix.TkVersion - # ============================================================ # Allowlist entries that cannot or should not be fixed; >= 3.9 # ============================================================ From e1673b4a0f4a503e2ae99d4edb686f410a0b7fe4 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Dec 2024 21:05:39 -0800 Subject: [PATCH 10/11] fix more --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/@tests/stubtest_allowlists/py38.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index f41fd98b730f..f294f7849473 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -4,7 +4,6 @@ # Please keep sorted alphabetically -asyncio.__all__ builtins.dict.get collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023 http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 3724db362d2b..15c6ce1f0b3d 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -59,6 +59,7 @@ tkinter.tix.wantobjects builtins.input # Incorrect default value in text signature, fixed in 3.10 collections.AsyncGenerator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.AsyncGenerator.aclose # async at runtime, deliberately not in the stub, see #7491 +collections.AsyncGenerator.asend # async at runtime, deliberately not in the stub, see #7491. Pos-only differences also. collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.ByteString # see comments in py3_common.txt collections.Callable From 2cd7e8ffa6e1800538057ff4dd3c6696d2d46a02 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Dec 2024 21:16:49 -0800 Subject: [PATCH 11/11] put more back --- stdlib/@tests/stubtest_allowlists/py39.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 6448c7ede697..d36dd74021ed 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -152,6 +152,14 @@ typing\.NamedTuple # Super-special typing primitive weakref.ProxyType.__reversed__ # Doesn't really exist +# ============================================================== +# Allowlist entries that cannot or should not be fixed; 3.9 only +# ============================================================== + +ast.FormattedValue.conversion # None on the class, but never None on instances +_ast.FormattedValue.conversion # None on the class, but never None on instances + + # ================================================================= # Allowlist entries that cannot or should not be fixed; 3.9 to 3.12 # =================================================================