Skip to content

Commit e62cb77

Browse files
[3.13] pythongh-123523: Rework typing documentation for generators and coroutines, and link to it from collections.abc docs (pythonGH-123544) (python#123790)
pythongh-123523: Rework typing documentation for generators and coroutines, and link to it from `collections.abc` docs (pythonGH-123544) (cherry picked from commit 56e4a41) Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com>
1 parent 9052888 commit e62cb77

File tree

2 files changed

+112
-101
lines changed

2 files changed

+112
-101
lines changed

Doc/library/collections.abc.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
216216

217217
ABC for classes that provide the :meth:`~object.__call__` method.
218218

219+
See :ref:`annotating-callables` for details on how to use
220+
:class:`!Callable` in type annotations.
221+
219222
.. class:: Iterable
220223

221224
ABC for classes that provide the :meth:`~container.__iter__` method.
@@ -253,6 +256,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
253256
:meth:`~generator.send`,
254257
:meth:`~generator.throw` and :meth:`~generator.close` methods.
255258

259+
See :ref:`annotating-generators-and-coroutines`
260+
for details on using :class:`!Generator` in type annotations.
261+
256262
.. versionadded:: 3.5
257263

258264
.. class:: Sequence
@@ -331,6 +337,11 @@ Collections Abstract Base Classes -- Detailed Descriptions
331337
Using ``isinstance(gencoro, Coroutine)`` for them will return ``False``.
332338
Use :func:`inspect.isawaitable` to detect them.
333339

340+
See :ref:`annotating-generators-and-coroutines`
341+
for details on using :class:`!Coroutine` in type annotations.
342+
The variance and order of type parameters correspond to those of
343+
:class:`Generator`.
344+
334345
.. versionadded:: 3.5
335346

336347
.. class:: AsyncIterable
@@ -352,6 +363,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
352363
ABC for :term:`asynchronous generator` classes that implement the protocol
353364
defined in :pep:`525` and :pep:`492`.
354365

366+
See :ref:`annotating-generators-and-coroutines`
367+
for details on using :class:`!AsyncGenerator` in type annotations.
368+
355369
.. versionadded:: 3.6
356370

357371
.. class:: Buffer

Doc/library/typing.rst

Lines changed: 98 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Annotating callable objects
208208
===========================
209209

210210
Functions -- or other :term:`callable` objects -- can be annotated using
211-
:class:`collections.abc.Callable` or :data:`typing.Callable`.
211+
:class:`collections.abc.Callable` or deprecated :data:`typing.Callable`.
212212
``Callable[[int], str]`` signifies a function that takes a single parameter
213213
of type :class:`int` and returns a :class:`str`.
214214

@@ -401,7 +401,7 @@ The type of class objects
401401
=========================
402402

403403
A variable annotated with ``C`` may accept a value of type ``C``. In
404-
contrast, a variable annotated with ``type[C]`` (or
404+
contrast, a variable annotated with ``type[C]`` (or deprecated
405405
:class:`typing.Type[C] <Type>`) may accept values that are classes
406406
themselves -- specifically, it will accept the *class object* of ``C``. For
407407
example::
@@ -441,6 +441,87 @@ For example::
441441
``type[Any]`` is equivalent to :class:`type`, which is the root of Python's
442442
:ref:`metaclass hierarchy <metaclasses>`.
443443

444+
445+
.. _annotating-generators-and-coroutines:
446+
447+
Annotating generators and coroutines
448+
====================================
449+
450+
A generator can be annotated using the generic type
451+
:class:`Generator[YieldType, SendType, ReturnType] <collections.abc.Generator>`.
452+
For example::
453+
454+
def echo_round() -> Generator[int, float, str]:
455+
sent = yield 0
456+
while sent >= 0:
457+
sent = yield round(sent)
458+
return 'Done'
459+
460+
Note that unlike many other generic classes in the standard library,
461+
the ``SendType`` of :class:`~collections.abc.Generator` behaves
462+
contravariantly, not covariantly or invariantly.
463+
464+
The ``SendType`` and ``ReturnType`` parameters default to :const:`!None`::
465+
466+
def infinite_stream(start: int) -> Generator[int]:
467+
while True:
468+
yield start
469+
start += 1
470+
471+
It is also possible to set these types explicitly::
472+
473+
def infinite_stream(start: int) -> Generator[int, None, None]:
474+
while True:
475+
yield start
476+
start += 1
477+
478+
Simple generators that only ever yield values can also be annotated
479+
as having a return type of either
480+
:class:`Iterable[YieldType] <collections.abc.Iterable>`
481+
or :class:`Iterator[YieldType] <collections.abc.Iterator>`::
482+
483+
def infinite_stream(start: int) -> Iterator[int]:
484+
while True:
485+
yield start
486+
start += 1
487+
488+
Async generators are handled in a similar fashion, but don't
489+
expect a ``ReturnType`` type argument
490+
(:class:`AsyncGenerator[YieldType, SendType] <collections.abc.AsyncGenerator>`).
491+
The ``SendType`` argument defaults to :const:`!None`, so the following definitions
492+
are equivalent::
493+
494+
async def infinite_stream(start: int) -> AsyncGenerator[int]:
495+
while True:
496+
yield start
497+
start = await increment(start)
498+
499+
async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
500+
while True:
501+
yield start
502+
start = await increment(start)
503+
504+
As in the synchronous case,
505+
:class:`AsyncIterable[YieldType] <collections.abc.AsyncIterable>`
506+
and :class:`AsyncIterator[YieldType] <collections.abc.AsyncIterator>` are
507+
available as well::
508+
509+
async def infinite_stream(start: int) -> AsyncIterator[int]:
510+
while True:
511+
yield start
512+
start = await increment(start)
513+
514+
Coroutines can be annotated using
515+
:class:`Coroutine[YieldType, SendType, ReturnType] <collections.abc.Coroutine>`.
516+
Generic arguments correspond to those of :class:`~collections.abc.Generator`,
517+
for example::
518+
519+
from collections.abc import Coroutine
520+
c: Coroutine[list[str], str, int] # Some coroutine defined elsewhere
521+
x = c.send('hi') # Inferred type of 'x' is list[str]
522+
async def bar() -> None:
523+
y = await c # Inferred type of 'y' is int
524+
444525
.. _user-defined-generics:
445526

446527
User-defined generic types
@@ -3318,14 +3399,9 @@ Aliases to built-in types
33183399
Deprecated alias to :class:`dict`.
33193400

33203401
Note that to annotate arguments, it is preferred
3321-
to use an abstract collection type such as :class:`Mapping`
3402+
to use an abstract collection type such as :class:`~collections.abc.Mapping`
33223403
rather than to use :class:`dict` or :class:`!typing.Dict`.
33233404

3324-
This type can be used as follows::
3325-
3326-
def count_words(text: str) -> Dict[str, int]:
3327-
...
3328-
33293405
.. deprecated:: 3.9
33303406
:class:`builtins.dict <dict>` now supports subscripting (``[]``).
33313407
See :pep:`585` and :ref:`types-genericalias`.
@@ -3335,16 +3411,9 @@ Aliases to built-in types
33353411
Deprecated alias to :class:`list`.
33363412

33373413
Note that to annotate arguments, it is preferred
3338-
to use an abstract collection type such as :class:`Sequence` or
3339-
:class:`Iterable` rather than to use :class:`list` or :class:`!typing.List`.
3340-
3341-
This type may be used as follows::
3342-
3343-
def vec2[T: (int, float)](x: T, y: T) -> List[T]:
3344-
return [x, y]
3345-
3346-
def keep_positives[T: (int, float)](vector: Sequence[T]) -> List[T]:
3347-
return [item for item in vector if item > 0]
3414+
to use an abstract collection type such as
3415+
:class:`~collections.abc.Sequence` or :class:`~collections.abc.Iterable`
3416+
rather than to use :class:`list` or :class:`!typing.List`.
33483417

33493418
.. deprecated:: 3.9
33503419
:class:`builtins.list <list>` now supports subscripting (``[]``).
@@ -3355,8 +3424,8 @@ Aliases to built-in types
33553424
Deprecated alias to :class:`builtins.set <set>`.
33563425

33573426
Note that to annotate arguments, it is preferred
3358-
to use an abstract collection type such as :class:`AbstractSet`
3359-
rather than to use :class:`set` or :class:`!typing.Set`.
3427+
to use an abstract collection type such as :class:`collections.abc.Set`
3428+
rather than to use :class:`set` or :class:`typing.Set`.
33603429

33613430
.. deprecated:: 3.9
33623431
:class:`builtins.set <set>` now supports subscripting (``[]``).
@@ -3552,11 +3621,6 @@ Aliases to container ABCs in :mod:`collections.abc`
35523621

35533622
Deprecated alias to :class:`collections.abc.Mapping`.
35543623

3555-
This type can be used as follows::
3556-
3557-
def get_position_in_index(word_list: Mapping[str, int], word: str) -> int:
3558-
return word_list[word]
3559-
35603624
.. deprecated:: 3.9
35613625
:class:`collections.abc.Mapping` now supports subscripting (``[]``).
35623626
See :pep:`585` and :ref:`types-genericalias`.
@@ -3620,14 +3684,9 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
36203684

36213685
Deprecated alias to :class:`collections.abc.Coroutine`.
36223686

3623-
The variance and order of type variables
3624-
correspond to those of :class:`Generator`, for example::
3625-
3626-
from collections.abc import Coroutine
3627-
c: Coroutine[list[str], str, int] # Some coroutine defined elsewhere
3628-
x = c.send('hi') # Inferred type of 'x' is list[str]
3629-
async def bar() -> None:
3630-
y = await c # Inferred type of 'y' is int
3687+
See :ref:`annotating-generators-and-coroutines`
3688+
for details on using :class:`collections.abc.Coroutine`
3689+
and ``typing.Coroutine`` in type annotations.
36313690

36323691
.. versionadded:: 3.5.3
36333692

@@ -3639,40 +3698,9 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
36393698

36403699
Deprecated alias to :class:`collections.abc.AsyncGenerator`.
36413700

3642-
An async generator can be annotated by the generic type
3643-
``AsyncGenerator[YieldType, SendType]``. For example::
3644-
3645-
async def echo_round() -> AsyncGenerator[int, float]:
3646-
sent = yield 0
3647-
while sent >= 0.0:
3648-
rounded = await round(sent)
3649-
sent = yield rounded
3650-
3651-
Unlike normal generators, async generators cannot return a value, so there
3652-
is no ``ReturnType`` type parameter. As with :class:`Generator`, the
3653-
``SendType`` behaves contravariantly.
3654-
3655-
The ``SendType`` defaults to :const:`!None`::
3656-
3657-
async def infinite_stream(start: int) -> AsyncGenerator[int]:
3658-
while True:
3659-
yield start
3660-
start = await increment(start)
3661-
3662-
It is also possible to set this type explicitly::
3663-
3664-
async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
3665-
while True:
3666-
yield start
3667-
start = await increment(start)
3668-
3669-
Alternatively, annotate your generator as having a return type of
3670-
either ``AsyncIterable[YieldType]`` or ``AsyncIterator[YieldType]``::
3671-
3672-
async def infinite_stream(start: int) -> AsyncIterator[int]:
3673-
while True:
3674-
yield start
3675-
start = await increment(start)
3701+
See :ref:`annotating-generators-and-coroutines`
3702+
for details on using :class:`collections.abc.AsyncGenerator`
3703+
and ``typing.AsyncGenerator`` in type annotations.
36763704

36773705
.. versionadded:: 3.6.1
36783706

@@ -3754,40 +3782,9 @@ Aliases to other ABCs in :mod:`collections.abc`
37543782

37553783
Deprecated alias to :class:`collections.abc.Generator`.
37563784

3757-
A generator can be annotated by the generic type
3758-
``Generator[YieldType, SendType, ReturnType]``. For example::
3759-
3760-
def echo_round() -> Generator[int, float, str]:
3761-
sent = yield 0
3762-
while sent >= 0:
3763-
sent = yield round(sent)
3764-
return 'Done'
3765-
3766-
Note that unlike many other generics in the typing module, the ``SendType``
3767-
of :class:`Generator` behaves contravariantly, not covariantly or
3768-
invariantly.
3769-
3770-
The ``SendType`` and ``ReturnType`` parameters default to :const:`!None`::
3771-
3772-
def infinite_stream(start: int) -> Generator[int]:
3773-
while True:
3774-
yield start
3775-
start += 1
3776-
3777-
It is also possible to set these types explicitly::
3778-
3779-
def infinite_stream(start: int) -> Generator[int, None, None]:
3780-
while True:
3781-
yield start
3782-
start += 1
3783-
3784-
Alternatively, annotate your generator as having a return type of
3785-
either ``Iterable[YieldType]`` or ``Iterator[YieldType]``::
3786-
3787-
def infinite_stream(start: int) -> Iterator[int]:
3788-
while True:
3789-
yield start
3790-
start += 1
3785+
See :ref:`annotating-generators-and-coroutines`
3786+
for details on using :class:`collections.abc.Generator`
3787+
and ``typing.Generator`` in type annotations.
37913788

37923789
.. deprecated:: 3.9
37933790
:class:`collections.abc.Generator` now supports subscripting (``[]``).

0 commit comments

Comments
 (0)