diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2017-02-04 06:18:42 (GMT) |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2017-02-04 06:18:42 (GMT) |
commit | 14169b2a89d62170d1ef9542429bfedb3158aa6c (patch) | |
tree | 91545cc22f3b870c3497ec30937623cf29e7d176 /Doc/library/typing.rst | |
parent | 3746619b84158dfdceaa837fc3e400a98d52fb17 (diff) | |
parent | 38962a6fe2e98e442f3fd586e4fd3d5f568b74fd (diff) | |
download | cpython-14169b2a89d62170d1ef9542429bfedb3158aa6c.zip cpython-14169b2a89d62170d1ef9542429bfedb3158aa6c.tar.gz cpython-14169b2a89d62170d1ef9542429bfedb3158aa6c.tar.bz2 |
Issue #29198: Merge from 3.5
Diffstat (limited to 'Doc/library/typing.rst')
-rw-r--r-- | Doc/library/typing.rst | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index efae67a..6c4af4c 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -689,6 +689,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 |