Title: TypeVar to represent a Callable's arguments · Issue #3028 · python/mypy · GitHub
Open Graph Title: TypeVar to represent a Callable's arguments · Issue #3028 · python/mypy
X Title: TypeVar to represent a Callable's arguments · Issue #3028 · python/mypy
Description: This is an idea for a new type system feature that can help more accurately type some decorators and wrapper functions, like @async() from https://github.com/quora/asynq. I'd like to see something like this work: from mypy_extensions imp...
Open Graph Description: This is an idea for a new type system feature that can help more accurately type some decorators and wrapper functions, like @async() from https://github.com/quora/asynq. I'd like to see something ...
X Description: This is an idea for a new type system feature that can help more accurately type some decorators and wrapper functions, like @async() from https://github.com/quora/asynq. I'd like to see someth...
Opengraph URL: https://github.com/python/mypy/issues/3028
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeVar to represent a Callable's arguments","articleBody":"This is an idea for a new type system feature that can help more accurately type some decorators and wrapper functions, like `@async()` from https://github.com/quora/asynq.\r\n\r\nI'd like to see something like this work:\r\n```python\r\nfrom mypy_extensions import ArgumentsVar\r\nfrom typing import TypeVar, Awaitable\r\n\r\n_ArgsT = ArgumentsVar('_ArgsT')\r\n_RetT = TypeVar('_RetT')\r\n\r\ndef make_awaitable(fn: Callable[_ArgsT, _RetT]) -\u003e Callable[_ArgsT, Awaitable[_RetT]]:\r\n async def wrapper(**args: _ArgsT, **kwargs: _ArgsT) -\u003e Awaitable[_RetT]:\r\n result = fn(*args, **kwargs)\r\n return result\r\n return wrapper\r\n\r\n@make_awaitable\r\ndef f(x: int, y: str) -\u003e int:\r\n return 3\r\n\r\nreveal_type(f(1, 'x')) # Awaitable[int]\r\nf(1, 1) # error, second argument must be str\r\nf(1, z=3) # error, z is not a valid kwarg\r\n```\r\nHaving a function's args and kwargs annotated with an `ArgumentsVar` would mean that it takes the arguments that the `ArgumentsVar` resolves to. As an extension, we could also make something like `def f(x: int, *args: _ArgsT, **kwargs: _ArgsT) -\u003e _T: ...` work to indicate that a function takes an argument called `x` plus the arguments specified in `_ArgsT`.\r\n\r\nThis could also improve some types in the standard library. For example, the annotations for `functools.singledispatch` currently don't check function arguments. With ArgumentsVar, it could be typed as follows:\r\n```python\r\n_T = TypeVar('_T')\r\n_ArgsT = ArgumentsVar('_ArgsT')\r\n\r\nclass _SingleDispatchCallable(Generic[_ArgsT, _T]):\r\n registry = ... # type: Mapping[Any, Callable[_ArgsT, _T]]\r\n def dispatch(self, cls: Any) -\u003e Callable[_ArgsT, _T]: ...\r\n @overload\r\n def register(self, cls: Any) -\u003e Callable[[Callable[_ArgsT, _T]], Callable[_ArgsT, _T]]: ...\r\n @overload\r\n def register(self, cls: Any, func: Callable[_ArgsT, _T]) -\u003e Callable[_ArgsT, _T]: ...\r\n def _clear_cache(self) -\u003e None: ...\r\n def __call__(self, *args: _ArgsT, **kwargs: _ArgsT) -\u003e _T: ...\r\n\r\ndef singledispatch(func: Callable[_ArgsT, _T]) -\u003e _SingleDispatchCallable[_ArgsT, _T]: ...\r\n```\r\nThis kind of thing has come up before, but I'm not sure a concrete solution has been proposed. I'll try to implement this to see how well it works.","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2017-03-19T17:38:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/3028/mypy/issues/3028"}
| 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:516295af-0a32-eb3c-78b5-d6a8c196c2be |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C362:3D6334:2216E60:313E989:6A4D12F5 |
| html-safe-nonce | f7113dc0c7b315471a1d31fd77714c78c2a48dd649260793ede76ad85ca06a39 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzYyOjNENjMzNDoyMjE2RTYwOjMxM0U5ODk6NkE0RDEyRjUiLCJ2aXNpdG9yX2lkIjoiNzQxNzYzNTMzNDkwMzM3MDQ4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 15925537762d8e2651cb6ffaa09778efd2f810d257da3d892eb1ec06ecafe6c2 |
| hovercard-subject-tag | issue:215280204 |
| 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/3028/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90429e1ce5f76779e37b3280fb043aa7bdc863574a28532ab602268fc53bf111/python/mypy/issues/3028 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90429e1ce5f76779e37b3280fb043aa7bdc863574a28532ab602268fc53bf111/python/mypy/issues/3028 |
| og:image:alt | This is an idea for a new type system feature that can help more accurately type some decorators and wrapper functions, like @async() from https://github.com/quora/asynq. I'd like to see something ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | f02420044fa3730a0607f4a52546bdd73f5ae60187ef57d284ecb6d7f3f1d880 |
| 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 | f0b6a62c1f0be1086cb0b5b077b82cebe520eb42 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width