Skip to content

Commit c7d0fd9

Browse files
authored
Make (Async)GeneratorType type parameters consistent with (Async)Generator type parameters (#14789)
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`
1 parent 19c8516 commit c7d0fd9

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

stdlib/types.pyi

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ class CellType:
392392
cell_contents: Any
393393

394394
_YieldT_co = TypeVar("_YieldT_co", covariant=True)
395-
_SendT_contra = TypeVar("_SendT_contra", contravariant=True)
396-
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True)
395+
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
396+
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
397397

398398
@final
399399
class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]):
@@ -450,8 +450,12 @@ class AsyncGeneratorType(AsyncGenerator[_YieldT_co, _SendT_contra]):
450450
def aclose(self) -> Coroutine[Any, Any, None]: ...
451451
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
452452

453+
# Non-default variations to accommodate coroutines
454+
_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True)
455+
_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)
456+
453457
@final
454-
class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]):
458+
class CoroutineType(Coroutine[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co]):
455459
__name__: str
456460
__qualname__: str
457461
@property
@@ -469,8 +473,8 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]):
469473
def cr_suspended(self) -> bool: ...
470474

471475
def close(self) -> None: ...
472-
def __await__(self) -> Generator[Any, None, _ReturnT_co]: ...
473-
def send(self, arg: _SendT_contra, /) -> _YieldT_co: ...
476+
def __await__(self) -> Generator[Any, None, _ReturnT_nd_co]: ...
477+
def send(self, arg: _SendT_nd_contra, /) -> _YieldT_co: ...
474478
@overload
475479
def throw(
476480
self, typ: type[BaseException], val: BaseException | object = ..., tb: TracebackType | None = ..., /

stdlib/typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ class Awaitable(Protocol[_T_co]):
577577
@abstractmethod
578578
def __await__(self) -> Generator[Any, Any, _T_co]: ...
579579

580-
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
580+
# Non-default variations to accommodate coroutines, and `AwaitableGenerator` having a 4th type parameter.
581581
_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True)
582582
_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)
583583

0 commit comments

Comments
 (0)