Title: Paramspec Mapped Types · Issue #1506 · python/typing · GitHub
Open Graph Title: Paramspec Mapped Types · Issue #1506 · python/typing
X Title: Paramspec Mapped Types · Issue #1506 · python/typing
Description: Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for. I'm writing a simple strongly typed dependency system, based on function closures. I have a decorator ...
Open Graph Description: Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for. I'm writing a simple strongly typed dependency system, based o...
X Description: Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for. I'm writing a simple strongly typed dependency sys...
Opengraph URL: https://github.com/python/typing/issues/1506
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Paramspec Mapped Types","articleBody":"Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for.\r\n\r\nI'm writing a simple strongly typed dependency system, based on function closures. I have a decorator function that has this typing:\r\n\r\n```python\r\n# This is a stand-in for a full class implementation that has a __call__ method matching\r\n# Callable[P, Awaitable[R]] - I'm just omitting it for brevity\r\ntype CallableObject[**P, R] = Callable[P, Awaitable[R]]\r\n\r\ndef dependency[**P, R](*args: P.args, **kwargs: P.kwargs) -> Callable[\r\n [Callable[P, Awaitable[R]] | Callable[P, R]], CallableObject\r\n]:\r\n ...\r\n```\r\n\r\nThis works to a degree, but, I want to transform the arguments in `P` to also accept functions that return the arguments type or an `Awaitable` of that type. For example:\r\n\r\n```python\r\ndef one() -> int:\r\n return 1\r\n\r\ndef two() -> int:\r\n return 2\r\n\r\n@dependency()\r\ndef three(one: int, two: int) -> int:\r\n return one + two\r\n\r\n# I want the decorated three to have this signature\r\ndef decorated_three(one: int | Callable[..., Awaitable[int] | int], two: int | Callable[..., Awaitable[int] | int]) -> int:\r\n ...\r\n```\r\n\r\nEssentially, I want to apply a mapping to every arg and kwarg in `P` that transforms it into `P | Callable[..., Awaitable[P] | P]`. Is this possible with the current grammar?\r\n\r\nAs a reference, I wrote an example that does what I'm attempting to do in typescript, where you can unpack what they call \"type tuples\" using the `keyof` syntax to mutate their values:\r\n\r\n```typescript\r\nfunction dependency\u003cP extends unknown[], R>(wrap: (...args: P) => R): (...args: {[V in keyof P]: P[V] | ((...args: any) => P[V] | Promise\u003cP[V]>)}) => R {\r\n return wrap as any\r\n}\r\n\r\nfunction one(): number { return 1 }\r\n\r\nfunction two(): number { return 2 }\r\n\r\nfunction three(one: number, two: number): number { return one + two }\r\n\r\n// Has type: const decorated_three: (one: number | ((...args: any) => number | Promise\u003cnumber>), two: number | ((...args: any) => number | Promise\u003cnumber>)) => number;\r\nconst decorated_three = dependency(three)\r\n```\r\n([Playground](https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAEwKYAdVjZAngHgAVFUAPKLZAZ0XAGsw4B3MAbQF0AaRAJQD4AFEwBOAQ3QAuRAIB0c0cIDmVKYQCUiALx9eaqbPlKViAN6sAaohhI6qXHGCJC7VRfaIAPtIMyFyqaJguBraTm6eTsJwALYwVKhEbnxqAL4hOjymAFCIuYjCqFAgwkgi4oiiNIG4WSlZWaCQsAiICKgCeohgINEARqjCpvmFxUgAjIh1DeDQ8EhQTHAdUt19A0MFRSWIAEyT9Y2zLVAAFgXtbSs9-cLcC3BXa8KdqzcbI9ttiADUiPf7WQA9IDEAAJSp-XCYKQQBBUKAoVCwsQUZAAfVO530ly613WXgEPj8xmq6VxTwihCisXi+FeA2Sd0WjzeBKJRgCQTJ9MGXipMTiCR5yW5eOEAG4srCwPDEcjRKiMWdUKgtIjMNgsBBcAJMSq1EA) where you can see / verify the typing works as expected)\r\n\r\nApologies for dragging in another type system/language - I'm just looking for the python equivalent (if it exists).\r\n\r\nAppreciate the help in advance. If this is a duplicate, please close it and mark it with the correct issue - I just couldn't find it 😅 \r\n","author":{"url":"https://github.com/cjntaylor","@type":"Person","name":"cjntaylor"},"datePublished":"2023-11-10T18:41:09Z","url":"https://github.com/python/typing/issues/1506"}
| 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:488efcff-8e6d-2418-44f6-cd90d2064980 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AFCA:D064:758930:9E4277:6AFF8FD8 |
| html-safe-nonce | 9a4a945cc8576727b7cdbf4dc2ffbd1d43920a98fb95fe5abd1e0e0aff9fdb61 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkNBOkQwNjQ6NzU4OTMwOjlFNDI3Nzo2QUZGOEZEOCIsInZpc2l0b3JfaWQiOiIyNjEwMjM5Njk1NjAyMjkwNjQ5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f1f2a078d8868c96e24c58afa17b3aa6022262d73f8f53e66dceb59da4b2dffd |
| hovercard-subject-tag | issue:1988165486 |
| 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/typing/1506/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f354fb6b514be1a040abf82bc255c2de0e9e7e1047fc7cb5b935ffdcade35ef6/python/typing/issues/1506 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f354fb6b514be1a040abf82bc255c2de0e9e7e1047fc7cb5b935ffdcade35ef6/python/typing/issues/1506 |
| og:image:alt | Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for. I'm writing a simple strongly typed dependency system, based o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cjntaylor |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2c75d045e8c84b51e2ac680086a6dd1eeaf2bc15b82137d22ee5106835d6256c |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | aa486b0389431f870c0adf9d4758aaff807c2d97 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width