Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1d3f66

Browse files
committed
Issue #29198: Merge from 3.6
2 parents 19d2597 + 14169b2 commit b1d3f66

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Doc/library/typing.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,39 @@ The module defines the following classes, functions and decorators:
689689
yield start
690690
start += 1
691691

692+
.. class:: AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra])
693+
694+
An async generator can be annotated by the generic type
695+
``AsyncGenerator[YieldType, SendType]``. For example::
696+
697+
async def echo_round() -> AsyncGenerator[int, float]:
698+
sent = yield 0
699+
while sent >= 0.0:
700+
rounded = await round(sent)
701+
sent = yield rounded
702+
703+
Unlike normal generators, async generators cannot return a value, so there
704+
is no ``ReturnType`` type parameter. As with :class:`Generator`, the
705+
``SendType`` behaves contravariantly.
706+
707+
If your generator will only yield values, set the ``SendType`` to
708+
``None``::
709+
710+
async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
711+
while True:
712+
yield start
713+
start = await increment(start)
714+
715+
Alternatively, annotate your generator as having a return type of
716+
either ``AsyncIterable[YieldType]`` or ``AsyncIterator[YieldType]``::
717+
718+
async def infinite_stream(start: int) -> AsyncIterator[int]:
719+
while True:
720+
yield start
721+
start = await increment(start)
722+
723+
.. versionadded:: 3.5.4
724+
692725
.. class:: Text
693726

694727
``Text`` is an alias for ``str``. It is provided to supply a forward

0 commit comments

Comments
 (0)