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

Skip to content

Commit 38962a6

Browse files
committed
Issue python#29198: Document typing.AsyncGenerator
Patch by Jelle Zijlstra.
1 parent db38b6c commit 38962a6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Doc/library/typing.rst

+33
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,39 @@ The module defines the following classes, functions and decorators:
670670
yield start
671671
start += 1
672672

673+
.. class:: AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra])
674+
675+
An async generator can be annotated by the generic type
676+
``AsyncGenerator[YieldType, SendType]``. For example::
677+
678+
async def echo_round() -> AsyncGenerator[int, float]:
679+
sent = yield 0
680+
while sent >= 0.0:
681+
rounded = await round(sent)
682+
sent = yield rounded
683+
684+
Unlike normal generators, async generators cannot return a value, so there
685+
is no ``ReturnType`` type parameter. As with :class:`Generator`, the
686+
``SendType`` behaves contravariantly.
687+
688+
If your generator will only yield values, set the ``SendType`` to
689+
``None``::
690+
691+
async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
692+
while True:
693+
yield start
694+
start = await increment(start)
695+
696+
Alternatively, annotate your generator as having a return type of
697+
either ``AsyncIterable[YieldType]`` or ``AsyncIterator[YieldType]``::
698+
699+
async def infinite_stream(start: int) -> AsyncIterator[int]:
700+
while True:
701+
yield start
702+
start = await increment(start)
703+
704+
.. versionadded:: 3.5.4
705+
673706
.. class:: Text
674707

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

0 commit comments

Comments
 (0)