summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2016-08-05 19:56:09 (GMT)
committerGuido van Rossum <guido@python.org>2016-08-05 19:56:09 (GMT)
commitb858af61b9bd2f58de0b6023002e9ce3df82b90d (patch)
tree96c536b488a1a71b5c6f83c18db719399934c7f7 /Doc
parent69332c1a64d6bdb80ff813fab403be832e12ce64 (diff)
downloadcpython-b858af61b9bd2f58de0b6023002e9ce3df82b90d.zip
cpython-b858af61b9bd2f58de0b6023002e9ce3df82b90d.tar.gz
cpython-b858af61b9bd2f58de0b6023002e9ce3df82b90d.tar.bz2
Add typing.Generator docs, by Michael Lee.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/typing.rst29
1 files changed, 29 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index d797aec..c870485 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -556,6 +556,35 @@ The module defines the following classes, functions and decorators:
.. class:: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
+ A generator can be annotated by the generic type
+ ``Generator[YieldType, SendType, ReturnType]``. For example::
+
+ def echo_round() -> Generator[int, float, str]:
+ sent = yield 0
+ while sent >= 0:
+ sent = yield round(sent)
+ return 'Done'
+
+ Note that unlike many other generics in the typing module, the ``SendType``
+ of :class:`Generator` behaves contravariantly, not covariantly or
+ invariantly.
+
+ If your generator will only yield values, set the ``SendType`` and
+ ``ReturnType`` to ``None``::
+
+ def infinite_stream(start: int) -> Generator[int, None, None]:
+ while True:
+ yield start
+ start += 1
+
+ Alternatively, annotate your generator as having a return type of
+ ``Iterator[YieldType]``::
+
+ def infinite_stream(start: int) -> Iterator[int]:
+ while True:
+ yield start
+ start += 1
+
.. class:: io
Wrapper namespace for I/O stream types.