File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -670,6 +670,39 @@ The module defines the following classes, functions and decorators:
670
670
yield start
671
671
start += 1
672
672
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
+
673
706
.. class :: Text
674
707
675
708
``Text `` is an alias for ``str ``. It is provided to supply a forward
You can’t perform that action at this time.
0 commit comments