Title: overloading __new__ · Issue #3307 · python/mypy · GitHub
Open Graph Title: overloading __new__ · Issue #3307 · python/mypy
X Title: overloading __new__ · Issue #3307 · python/mypy
Description: I propose allowing the use of @overload on __new__ to specify the return type. E.g.: T = TypeVar('T') class MyClass(Generic[T]): @overload def __new__(cls,val: str) -> 'MyClass[int]': pass @overload def __new__(cls,val: int) -> int: pass...
Open Graph Description: I propose allowing the use of @overload on __new__ to specify the return type. E.g.: T = TypeVar('T') class MyClass(Generic[T]): @overload def __new__(cls,val: str) -> 'MyClass[int]': pass @overloa...
X Description: I propose allowing the use of @overload on __new__ to specify the return type. E.g.: T = TypeVar('T') class MyClass(Generic[T]): @overload def __new__(cls,val: str) -> 'MyClass[int]&...
Opengraph URL: https://github.com/python/mypy/issues/3307
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"overloading __new__","articleBody":"I propose allowing the use of ``@overload`` on ``__new__`` to specify the return type. E.g.:\r\n```\r\nT = TypeVar('T')\r\n\r\nclass MyClass(Generic[T]):\r\n @overload\r\n def __new__(cls,val: str) -> 'MyClass[int]':\r\n pass\r\n @overload\r\n def __new__(cls,val: int) -> int:\r\n pass\r\n \r\n def __new__(cls,val):\r\n if isinstance(val,int):\r\n return val\r\n r = super().__init__(cls)\r\n if isinstance(val,str):\r\n r.val = int(val)\r\n r.val = val\r\n return r\r\n```\r\nThen, if ``@overload`` is allowed to have return types that only differ by the generic type argument (e.g. ``MyClass[int]`` vs ``MyClass[str]``), this would allow specifying default type arguments (which is already requested in https://github.com/python/typing/issues/307) as well as more complicated type argument deduction, such as when a certain type gets cast to another type (not with the ``cast`` function but like in the above code with ``str`` to ``int``).\r\n\r\nAnd if this is allowed, I don't see why it shouldn't be permissible to specify a return type that isn't an instance of the containing class. This is not essential, but it's something Python allows. I have used it for automatically simplifying expressions in a domain-specific language and [SymPy](http://www.sympy.org) does this with symbolic math:\r\n```\r\n>>> from sympy import *\r\n>>> x = Symbol('x')\r\n>>> Pow\r\n\u003cclass 'sympy.core.power.Pow'>\r\n>>> type(Pow(x,1))\r\n\u003cclass 'sympy.core.symbol.Symbol'>\r\n```\r\nIn such cases, it might be useful to mark the class return type as a super-class of itself.","author":{"url":"https://github.com/Rouslan","@type":"Person","name":"Rouslan"},"datePublished":"2017-05-03T11:39:37Z","url":"https://github.com/python/mypy/issues/3307"}
| 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:bfc6df99-8443-9412-3b29-eff67e26255c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAB2:77A5A:2C71A99:3ABCD84:6B001F1B |
| html-safe-nonce | 80aa1a56e2797a12536fd32d5b5f4c0be54a235f5533282eecd6691949b51004 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUIyOjc3QTVBOjJDNzFBOTk6M0FCQ0Q4NDo2QjAwMUYxQiIsInZpc2l0b3JfaWQiOiI4Mjk0Njg3MzQwNTkzNDIyMTA3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6dec3982868d1618c4b315db0a99ebff539f42546c3270e2cbbdbd903cdc98b4 |
| hovercard-subject-tag | issue:225957832 |
| 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/3307/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bbfb43185cb740351c912fdd68d1cba55f2c7d72d664314970163b3330508331/python/mypy/issues/3307 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bbfb43185cb740351c912fdd68d1cba55f2c7d72d664314970163b3330508331/python/mypy/issues/3307 |
| og:image:alt | I propose allowing the use of @overload on __new__ to specify the return type. E.g.: T = TypeVar('T') class MyClass(Generic[T]): @overload def __new__(cls,val: str) -> 'MyClass[int]': pass @overloa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Rouslan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width