diff -r da9898e7e90d -r f43872f8359a Doc/library/typing.rst --- a/Doc/library/typing.rst Wed Jul 27 16:59:22 2016 +0200 +++ b/Doc/library/typing.rst Thu Aug 04 21:06:14 2016 -0700 @@ -466,6 +466,35 @@ .. 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 is not None: + 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.