Skip to content

Commit 0974b0a

Browse files
committed
Use TypeVar defaults for Generator and AsyncGenerator
1 parent e436dfe commit 0974b0a

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

stdlib/typing.pyi

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ class Reversible(Iterable[_T_co], Protocol[_T_co]):
402402
def __reversed__(self) -> Iterator[_T_co]: ...
403403

404404
_YieldT_co = TypeVar("_YieldT_co", covariant=True)
405-
_SendT_contra = TypeVar("_SendT_contra", contravariant=True)
406-
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True)
405+
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
406+
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
407407

408408
class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]):
409409
def __next__(self) -> _YieldT_co: ...
@@ -445,7 +445,11 @@ class Awaitable(Protocol[_T_co]):
445445
@abstractmethod
446446
def __await__(self) -> Generator[Any, Any, _T_co]: ...
447447

448-
class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]):
448+
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
449+
_SendT_contra_nd = TypeVar("_SendT_contra_nd", contravariant=True)
450+
_ReturnT_co_nd = TypeVar("_ReturnT_co_nd", covariant=True)
451+
452+
class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd]):
449453
__name__: str
450454
__qualname__: str
451455
@property
@@ -457,7 +461,7 @@ class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _Retu
457461
@property
458462
def cr_running(self) -> bool: ...
459463
@abstractmethod
460-
def send(self, value: _SendT_contra, /) -> _YieldT_co: ...
464+
def send(self, value: _SendT_contra_nd, /) -> _YieldT_co: ...
461465
@overload
462466
@abstractmethod
463467
def throw(
@@ -473,9 +477,9 @@ class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _Retu
473477
# The parameters correspond to Generator, but the 4th is the original type.
474478
@type_check_only
475479
class AwaitableGenerator(
476-
Awaitable[_ReturnT_co],
477-
Generator[_YieldT_co, _SendT_contra, _ReturnT_co],
478-
Generic[_YieldT_co, _SendT_contra, _ReturnT_co, _S],
480+
Awaitable[_ReturnT_co_nd],
481+
Generator[_YieldT_co_nd, _SendT_contra_nd, _ReturnT_co_nd],
482+
Generic[_YieldT_co_nd, _SendT_contra_nd, _ReturnT_co_nd, _S],
479483
metaclass=ABCMeta,
480484
): ...
481485

0 commit comments

Comments
 (0)