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

Add coroutines to the cheatsheet #4677

Merged
merged 6 commits into from Apr 4, 2018

Conversation

Projects
None yet
5 participants
@rkday
Contributor

rkday commented Mar 4, 2018

It took me a while to find http://mypy.readthedocs.io/en/stable/kinds_of_types.html#typing-async-await in the documentation, and it would have been easier to find if it was mentioned in the cheatsheet - this adds both @asyncio.coroutine and async def examples to the Python 3 cheatsheet.

I think it's right that the YieldType of a Python 3.4 generator-coroutine should always be Any, and the SendType always None, but if there are exceptions let me know and I can tweak the text.

@@ -94,6 +94,36 @@ Python 3 introduces an annotation syntax for function declarations in `PEP 3107
yield i
i += 1
# A Python 3.4 coroutine should have a return type of
# Generator[Any, None, T], where T is the type it returns.
@asyncio.coroutine

This comment has been minimized.

@ethanhs

ethanhs Mar 4, 2018

Collaborator

I think we try to keep the cheat sheet copy/paste runnable. Could you add an import for asyncio?

This comment has been minimized.

@rkday

rkday Mar 4, 2018

Contributor

Sure! I've fixed up the imports, and also fixed the duplicate names that got thrown up when typechecking, so this now executes cleanly and the only error I get is the expected error for quux that was already there.

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 5, 2018

Since this is still a somewhat esoteric topic, can you make it a separate section (e.g. named "Couroutines and asyncio") after the "Functions" section? (I.e. right before "puzzled".)

@rkday

This comment has been minimized.

Contributor

rkday commented Mar 6, 2018

Yep, done.

@ethanhs

Thanks for working on this, just a few more nits.

return "placeholder"
# A Python 3.5+ coroutine is typed like a normal function, and doesn't need
# a 'yield' to make it typecheck.

This comment has been minimized.

@ethanhs

ethanhs Mar 8, 2018

Collaborator

I don't think the and doesn't need a 'yield' to make it typecheck is needed. Coroutines don't need to be generator functions.

import asyncio
from typing import Generator, Any
# A Python 3.4 coroutine should have a return type of

This comment has been minimized.

@ethanhs

ethanhs Mar 8, 2018

Collaborator

I think it would be better to say a "generator based coroutine" or "async generator" here instead of Python 3.4 coroutine which is a bit ambiguous.

return "Blastoff!"
# mypy currently does not support converting functions into coroutines in
# Python 3.4, so you need to add a 'yield' to make it typecheck.

This comment has been minimized.

@ethanhs

ethanhs Mar 8, 2018

Collaborator

I think it is important to say "dynamically convert a function into an async generator" as mypy can infer explicit generators are generators.

@ethanhs

This comment has been minimized.

Collaborator

ethanhs commented Mar 27, 2018

@rkday if you have time to respond to my remaining comments, it would be appreciated!

@rkday

This comment has been minimized.

Contributor

rkday commented Mar 27, 2018

Sure - how's this?

@@ -125,16 +125,15 @@ See :ref:`async-and-await` for the full detail on typing coroutines and asynchro
count -= 1
return "Blastoff!"
# mypy currently does not support converting functions into coroutines in
# Python 3.4, so you need to add a 'yield' to make it typecheck.
# mypy currently does not support converting functions into async generators

This comment has been minimized.

@JelleZijlstra

JelleZijlstra Mar 27, 2018

Collaborator

Async generators are a different feature that was added in 3.6. Do you mean generator-based coroutines?

This comment has been minimized.

@ethanhs

ethanhs Mar 27, 2018

Collaborator

Yeah that is my bad. I suggested it and forgot about PEP 525. "generator based coroutine" is probably better.

This comment has been minimized.

@rkday

rkday Mar 28, 2018

Contributor

Fixed.

@ethanhs

Thanks so much!

@msullivan msullivan merged commit 5ce23c6 into python:master Apr 4, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment