diff options
author | Guido van Rossum <guido@dropbox.com> | 2016-08-05 19:57:38 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2016-08-05 19:57:38 (GMT) |
commit | b6337a11450bafda034c518a8363c5c58d409f7c (patch) | |
tree | d1564fa264d92bc66adc2ab058955e21d8b662a6 /Doc/library/typing.rst | |
parent | 02b75abf731831e32bbb8007a3278c14f6ad700a (diff) | |
parent | b858af61b9bd2f58de0b6023002e9ce3df82b90d (diff) | |
download | cpython-b6337a11450bafda034c518a8363c5c58d409f7c.zip cpython-b6337a11450bafda034c518a8363c5c58d409f7c.tar.gz cpython-b6337a11450bafda034c518a8363c5c58d409f7c.tar.bz2 |
Add typing.Generator docs, by Michael Lee. (Merge 3.5->3.6)
Diffstat (limited to 'Doc/library/typing.rst')
-rw-r--r-- | Doc/library/typing.rst | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 7f54e19..57e5c65 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -561,6 +561,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. |