summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2017-02-04 09:18:11 +0300
committerBerker Peksag <berker.peksag@gmail.com>2017-02-04 09:18:11 +0300
commit50a3761c130e9be725bacb5b99d624012c40414a (patch)
treee442b6826346b148b8f3be08177426b09f49b7b8
parentef37141cf58ef24b0096f9ab7fe888b39167f9ec (diff)
downloadcpython-50a3761c130e9be725bacb5b99d624012c40414a.tar.gz
Issue #29198: Document typing.AsyncGenerator
Patch by Jelle Zijlstra.
-rw-r--r--Doc/library/typing.rst33
1 files changed, 33 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index f89d886cd7..aaa43e4a44 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -670,6 +670,39 @@ The module defines the following classes, functions and decorators:
yield start
start += 1
+.. class:: AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra])
+
+ An async generator can be annotated by the generic type
+ ``AsyncGenerator[YieldType, SendType]``. For example::
+
+ async def echo_round() -> AsyncGenerator[int, float]:
+ sent = yield 0
+ while sent >= 0.0:
+ rounded = await round(sent)
+ sent = yield rounded
+
+ Unlike normal generators, async generators cannot return a value, so there
+ is no ``ReturnType`` type parameter. As with :class:`Generator`, the
+ ``SendType`` behaves contravariantly.
+
+ If your generator will only yield values, set the ``SendType`` to
+ ``None``::
+
+ async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
+ while True:
+ yield start
+ start = await increment(start)
+
+ Alternatively, annotate your generator as having a return type of
+ either ``AsyncIterable[YieldType]`` or ``AsyncIterator[YieldType]``::
+
+ async def infinite_stream(start: int) -> AsyncIterator[int]:
+ while True:
+ yield start
+ start = await increment(start)
+
+ .. versionadded:: 3.5.4
+
.. class:: Text
``Text`` is an alias for ``str``. It is provided to supply a forward