-
Notifications
You must be signed in to change notification settings - Fork 258
Short subsection on annotating coroutines #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1015,6 +1015,40 @@ In such cases the default value may be specified as a literal | |
ellipsis, i.e. the above example is literally what you would write. | ||
|
||
|
||
Annotating generator functions and coroutines | ||
--------------------------------------------- | ||
|
||
The return type of generator functions can be annotated by | ||
the generic type ``Generator[yield_type, send_type, | ||
return_type]`` provided by ``typing.py`` module:: | ||
|
||
def echo_round() -> Generator[int, float, str]: | ||
res = yield | ||
while res: | ||
res = yield round(res) | ||
return 'OK' | ||
|
||
Coroutines introduced in PEP 492 are annotated with the same syntax as | ||
ordinary functions. However, the return type annotation corresponds to the | ||
type of ``await`` expression, not to the coroutine type:: | ||
|
||
async def spam(ignored: int) -> str: | ||
return 'spam' | ||
|
||
async def foo(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoa, I think this needs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the initial version I had this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just updated the PEP. I think it's better to add, because the only known On Thu, May 26, 2016 at 12:37 AM, Ivan Levkivskyi [email protected]
--Guido van Rossum (python.org/~guido) |
||
bar = await spam(42) # type: str | ||
|
||
The ``typing.py`` module also provides generic ABCs ``Awaitable``, | ||
``AsyncIterable``, and ``AsyncIterator`` for situations where more precise | ||
types cannot be specified:: | ||
|
||
def op() -> typing.Awaitable[str]: | ||
if cond: | ||
return spam(42) | ||
else: | ||
return asyncio.Future(...) | ||
|
||
|
||
Compatibility with other uses of function annotations | ||
===================================================== | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From this example alone it's hard to see how [int, float, str] map to the various aspects of generators. Personally I use the fact that they were introduced historically at different times: yielded value (int), value returned by yield expression (float), value returned by return statement (str). But for others that may not be a great help...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I added the type parameters in a new commit.