Title: Use self-type in `__init__` to add extra generic types · Issue #16752 · python/mypy · GitHub
Open Graph Title: Use self-type in `__init__` to add extra generic types · Issue #16752 · python/mypy
X Title: Use self-type in `__init__` to add extra generic types · Issue #16752 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use self-type in `__init__` to add extra generic types","articleBody":"**Feature**\r\n\r\n\u003c!-- Enter a clear and concise description of your feature proposal here. -->\r\n\r\nAllow using self-types to add intermediate generic types for `__init__`.\r\n\r\n**Pitch**\r\n\r\n\u003c!-- Please explain why this feature should be implemented and how it would be used. Add examples, if applicable. -->\r\n\r\nThis would make https://github.com/python-trio/trio/pull/2898 possible to type well without the awful hacks we had to do. Specifically making sure `RaisesGroup(RaisesGroup(ValueError))` is `RaisesGroup[ExceptionGroup[ValueError]]`.\r\n\r\nPerhaps a code sample would make more sense:\r\n\r\n```py\r\nfrom typing import Generic, TypeVar, overload\r\n\r\nE = TypeVar(\"E\", bound=BaseException)\r\nE2 = TypeVar(\"E2\", bound=BaseException)\r\n\r\nclass RaisesGroup(Generic[E]):\r\n @overload\r\n def __init__(self, a: type[E]):\r\n ...\r\n \r\n @overload\r\n def __init__(self: \"RaisesGroup[BaseExceptionGroup[E2]]\", a: \"RaisesGroup[E2]\"):\r\n ...\r\n \r\n def __init__(self, a: object):\r\n ...\r\n\r\n\r\nRaisesGroup(RaisesGroup(ValueError)) # currently: Argument 1 to \"RaisesGroup\" has incompatible type \"RaisesGroup[ValueError]\"; expected \"RaisesGroup[Never]\"\r\n```","author":{"url":"https://github.com/A5rocks","@type":"Person","name":"A5rocks"},"datePublished":"2024-01-06T03:51:28Z","url":"https://github.com/python/mypy/issues/16752"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:0ae1b2a9-0846-2886-f44b-9be42b49870e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 983C:1FBEEF:EC8AF3:1327F94:6AFFEE68 |
| html-safe-nonce | 97e15e9a32d29a78db3988218d8951f75208c56678fde82ed371eb64c007c1ee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODNDOjFGQkVFRjpFQzhBRjM6MTMyN0Y5NDo2QUZGRUU2OCIsInZpc2l0b3JfaWQiOiIzNjUyODM5NDQxODgzNDU5MTc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 05fdc0b03b202f2d5b0cbf8e5a1f115568bfd40e6a12b0fc264322b4e91b8082 |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/16752 |
| twitter:image | https://opengraph.githubassets.com/850ce2019117db99c166b712132547c2dade118d4084179fa6a2ee39e584b4de/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/850ce2019117db99c166b712132547c2dade118d4084179fa6a2ee39e584b4de/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-cache |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width