Skip to content

Commit 3195f9f

Browse files
authored
Use TypeVar defaults for Generator and AsyncGenerator (#11867)
1 parent 708656f commit 3195f9f

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
@@ -401,8 +401,8 @@ class Reversible(Iterable[_T_co], Protocol[_T_co]):
401401
def __reversed__(self) -> Iterator[_T_co]: ...
402402

403403
_YieldT_co = TypeVar("_YieldT_co", covariant=True)
404-
_SendT_contra = TypeVar("_SendT_contra", contravariant=True)
405-
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True)
404+
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None)
405+
_ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None)
406406

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

447-
class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _ReturnT_co]):
447+
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
448+
_SendT_contra_nd = TypeVar("_SendT_contra_nd", contravariant=True)
449+
_ReturnT_co_nd = TypeVar("_ReturnT_co_nd", covariant=True)
450+
451+
class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd]):
448452
__name__: str
449453
__qualname__: str
450454
@property
@@ -456,7 +460,7 @@ class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _Retu
456460
@property
457461
def cr_running(self) -> bool: ...
458462
@abstractmethod
459-
def send(self, value: _SendT_contra, /) -> _YieldT_co: ...
463+
def send(self, value: _SendT_contra_nd, /) -> _YieldT_co: ...
460464
@overload
461465
@abstractmethod
462466
def throw(
@@ -472,9 +476,9 @@ class Coroutine(Awaitable[_ReturnT_co], Generic[_YieldT_co, _SendT_contra, _Retu
472476
# The parameters correspond to Generator, but the 4th is the original type.
473477
@type_check_only
474478
class AwaitableGenerator(
475-
Awaitable[_ReturnT_co],
476-
Generator[_YieldT_co, _SendT_contra, _ReturnT_co],
477-
Generic[_YieldT_co, _SendT_contra, _ReturnT_co, _S],
479+
Awaitable[_ReturnT_co_nd],
480+
Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd],
481+
Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd, _S],
478482
metaclass=ABCMeta,
479483
): ...
480484

0 commit comments

Comments
 (0)