From 15148b548219b3ad22028218dd348bdfaf6306c3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 26 Sep 2025 12:00:08 +0100 Subject: [PATCH 1/3] Make `GeneratorType` type parameters consistent with `Generator` type parameters Currently `Generator` is generic over three type variables that have defaults, but `GeneratorType` is generic over three type variables that do not have defaults. It seems like it probably makes more sense for them to be consistent? The vast majority of real-world `Generator`s are instances of `GeneratorType` --- stdlib/types.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index e45b3781352b..e14ebd0a32ca 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -392,8 +392,8 @@ class CellType: cell_contents: Any _YieldT_co = TypeVar("_YieldT_co", covariant=True) -_SendT_contra = TypeVar("_SendT_contra", contravariant=True) -_ReturnT_co = TypeVar("_ReturnT_co", covariant=True) +_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None) +_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None) @final class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]): From a64adce25f841bf93de491e3d5b2c2360007756e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 26 Sep 2025 12:04:45 +0100 Subject: [PATCH 2/3] oops not you, `Coroutine` --- stdlib/types.pyi | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index e14ebd0a32ca..c3d3b6b4dad5 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -450,8 +450,12 @@ class AsyncGeneratorType(AsyncGenerator[_YieldT_co, _SendT_contra]): def aclose(self) -> Coroutine[Any, Any, None]: ... def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... +# Non-default variations to accommodate couroutines +_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True) +_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True) + @final -class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]): +class CoroutineType(Coroutine[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co]): __name__: str __qualname__: str @property @@ -469,8 +473,8 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]): def cr_suspended(self) -> bool: ... def close(self) -> None: ... - def __await__(self) -> Generator[Any, None, _ReturnT_co]: ... - def send(self, arg: _SendT_contra, /) -> _YieldT_co: ... + def __await__(self) -> Generator[Any, None, _ReturnT_nd_co]: ... + def send(self, arg: _SendT_nd_contra, /) -> _YieldT_co: ... @overload def throw( self, typ: type[BaseException], val: BaseException | object = ..., tb: TracebackType | None = ..., / From a4e62a3447334735739204b9d2475280e0b033db Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 26 Sep 2025 12:06:01 +0100 Subject: [PATCH 3/3] couroutines --- stdlib/types.pyi | 2 +- stdlib/typing.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index c3d3b6b4dad5..e55dcfc5f3c2 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -450,7 +450,7 @@ class AsyncGeneratorType(AsyncGenerator[_YieldT_co, _SendT_contra]): def aclose(self) -> Coroutine[Any, Any, None]: ... def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... -# Non-default variations to accommodate couroutines +# Non-default variations to accommodate coroutines _SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True) _ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 7a683a953b46..ca25c92d5c34 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -577,7 +577,7 @@ class Awaitable(Protocol[_T_co]): @abstractmethod def __await__(self) -> Generator[Any, Any, _T_co]: ... -# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter. +# Non-default variations to accommodate coroutines, and `AwaitableGenerator` having a 4th type parameter. _SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True) _ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)