Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b858af61
Commit
b858af61
authored
Aug 05, 2016
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add typing.Generator docs, by Michael Lee.
parent
69332c1a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
0 deletions
+29
-0
Doc/library/typing.rst
Doc/library/typing.rst
+29
-0
No files found.
Doc/library/typing.rst
View file @
b858af61
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment