@@ -208,7 +208,7 @@ Annotating callable objects
208
208
===========================
209
209
210
210
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 `.
212
212
``Callable[[int], str] `` signifies a function that takes a single parameter
213
213
of type :class: `int ` and returns a :class: `str `.
214
214
@@ -401,7 +401,7 @@ The type of class objects
401
401
=========================
402
402
403
403
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
405
405
:class: `typing.Type[C] <Type> `) may accept values that are classes
406
406
themselves -- specifically, it will accept the *class object * of ``C ``. For
407
407
example::
@@ -441,6 +441,87 @@ For example::
441
441
``type[Any] `` is equivalent to :class: `type `, which is the root of Python's
442
442
:ref: `metaclass hierarchy <metaclasses >`.
443
443
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
+
444
525
.. _user-defined-generics :
445
526
446
527
User-defined generic types
@@ -3318,14 +3399,9 @@ Aliases to built-in types
3318
3399
Deprecated alias to :class: `dict `.
3319
3400
3320
3401
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 `
3322
3403
rather than to use :class: `dict ` or :class: `!typing.Dict `.
3323
3404
3324
- This type can be used as follows::
3325
-
3326
- def count_words(text: str) -> Dict[str, int]:
3327
- ...
3328
-
3329
3405
.. deprecated :: 3.9
3330
3406
:class: `builtins.dict <dict> ` now supports subscripting (``[] ``).
3331
3407
See :pep: `585 ` and :ref: `types-genericalias `.
@@ -3335,16 +3411,9 @@ Aliases to built-in types
3335
3411
Deprecated alias to :class: `list `.
3336
3412
3337
3413
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 `.
3348
3417
3349
3418
.. deprecated :: 3.9
3350
3419
:class: `builtins.list <list> ` now supports subscripting (``[] ``).
@@ -3355,8 +3424,8 @@ Aliases to built-in types
3355
3424
Deprecated alias to :class: `builtins.set <set> `.
3356
3425
3357
3426
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 `.
3360
3429
3361
3430
.. deprecated :: 3.9
3362
3431
:class: `builtins.set <set> ` now supports subscripting (``[] ``).
@@ -3552,11 +3621,6 @@ Aliases to container ABCs in :mod:`collections.abc`
3552
3621
3553
3622
Deprecated alias to :class: `collections.abc.Mapping `.
3554
3623
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
-
3560
3624
.. deprecated :: 3.9
3561
3625
:class: `collections.abc.Mapping ` now supports subscripting (``[] ``).
3562
3626
See :pep: `585 ` and :ref: `types-genericalias `.
@@ -3620,14 +3684,9 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
3620
3684
3621
3685
Deprecated alias to :class: `collections.abc.Coroutine `.
3622
3686
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.
3631
3690
3632
3691
.. versionadded :: 3.5.3
3633
3692
@@ -3639,40 +3698,9 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
3639
3698
3640
3699
Deprecated alias to :class: `collections.abc.AsyncGenerator `.
3641
3700
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.
3676
3704
3677
3705
.. versionadded :: 3.6.1
3678
3706
@@ -3754,40 +3782,9 @@ Aliases to other ABCs in :mod:`collections.abc`
3754
3782
3755
3783
Deprecated alias to :class: `collections.abc.Generator `.
3756
3784
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.
3791
3788
3792
3789
.. deprecated :: 3.9
3793
3790
:class: `collections.abc.Generator ` now supports subscripting (``[] ``).
0 commit comments