Title: Design for using async+await with asyncio · Issue #1886 · python/mypy · GitHub
Open Graph Title: Design for using async+await with asyncio · Issue #1886 · python/mypy
X Title: Design for using async+await with asyncio · Issue #1886 · python/mypy
Description: I'm trying something new here, pouring my thoughts into the issue tracker to get some clarity. I hope that we'll soon have syntactic support for PEP 492, in particular async def and await (#1808). (Yes, it also supports async for and asy...
Open Graph Description: I'm trying something new here, pouring my thoughts into the issue tracker to get some clarity. I hope that we'll soon have syntactic support for PEP 492, in particular async def and await (#1808). ...
X Description: I'm trying something new here, pouring my thoughts into the issue tracker to get some clarity. I hope that we'll soon have syntactic support for PEP 492, in particular async def and await (...
Opengraph URL: https://github.com/python/mypy/issues/1886
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Design for using async+await with asyncio","articleBody":"I'm trying something new here, pouring my thoughts into the issue tracker to get some clarity.\n\nI hope that we'll soon have syntactic support for PEP 492, in particular `async def` and `await` (#1808). (Yes, it also supports `async for` and `async with`, but these aren't very interesting so I won't mention them.)\n\nI've also made some improvements to the asyncio stubs (https://github.com/python/typeshed/pull/373, merged already) and created a fully annotated example, crawl.py (#1878). But these do not use `async def` or `await`; they use generators annotated with `@asyncio.coroutine` and `yield from` to wait for coroutines and Futures.\n\nThe next step is supporting asyncio code that uses `async def` and `await`, and here I am a little stuck. On the one hand, PEP 492 draws a clear distinction between generators and native coroutines (the latter being the ones defined using `async def`). You can't use a generator with `await`, and you can't use a native coroutine with `yield from`. On the other hand, PEP 492 also allows an escape clause: by using the decorator `@types.coroutine` you can mark a generator as a coroutine, and then `await` accepts it. The `@asyncio.coroutine` decorator calls this decorator, in addition to doing other asyncio-specific things (some of which are only apparent in debug mode).\n\nI would like mypy to model all this as well as possible, so that mypy will give a useful error message when you try to use a generator with `await` or a coroutine with `yield from`. But it should also understand that a generator decorated with `@types.coroutine` or `@asyncio.coroutine` is acceptable for `await` as well as for `yield from` (in the latter case it would be even better if it could also keep track of whether the generator containing the `yield from` is decorated with `@asyncio.coroutine`).\n\nThe final piece of the puzzle (or rather, contribution to the problem) is that it's not easy to specify the type for a decorator that modifies the signature of the function it wraps. What I wish I could do is to declare that the `@asyncio.coroutine` decorator takes a function with arguments `AA` and return type `Generator[Any, Any, R]` and returns a function with arguments `AA` and return type `Awaitable[R]` (which is what `await` requires -- the return value of `await` is then of type `R`). And I'd like the decorator to be overloaded so that it also handles a few other cases.\n\nBut neither PEP 484 nor mypy supports this \"signature algebra\", so the best I can think of is to special-case the two decorators (`@types.coroutine` and `@asyncio.coroutine`) in mypy, in a similar fashion as how it deals with some other built-in decorators (`@property`, `@classmethod` and probably a few more).\n\n[To be continued]\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2016-07-16T18:49:26Z","url":"https://github.com/python/mypy/issues/1886"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2a5ac111-3eec-02e5-1266-3b5dc0cdddbc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B156:1A1C04:2373341:3064EF5:6B002FE3 |
| html-safe-nonce | d3f5e473a2570daedf855f597b17da625f223e3f3c133f55a78c46deee4205d8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMTU2OjFBMUMwNDoyMzczMzQxOjMwNjRFRjU6NkIwMDJGRTMiLCJ2aXNpdG9yX2lkIjoiNDQxODM0ODI4MTExODE4NzI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d9f5798bfec8005afd64de7c7117983cf008beca13599bac7ba1f65957480940 |
| hovercard-subject-tag | issue:165942362 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/1886/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e2336178c59f7657b4044006eb1766857dedde6c549776e95e367f6964e2d6e7/python/mypy/issues/1886 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e2336178c59f7657b4044006eb1766857dedde6c549776e95e367f6964e2d6e7/python/mypy/issues/1886 |
| og:image:alt | I'm trying something new here, pouring my thoughts into the issue tracker to get some clarity. I hope that we'll soon have syntactic support for PEP 492, in particular async def and await (#1808). ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width