Title: `__new__` return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class · Issue #19624 · python/mypy · GitHub
Open Graph Title: `__new__` return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class · Issue #19624 · python/mypy
X Title: `__new__` return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class · Issue #19624 · python/mypy
Description: new.py: from typing import cast class A: def __new__(cls) -> int: return 1 class B: def __new__(cls) -> "C": return cast(C, None) class C(B): def __new__(cls) -> B: return cast(B, None) reveal_type(A()) # Expect int (builtin.int) reveal_...
Open Graph Description: new.py: from typing import cast class A: def __new__(cls) -> int: return 1 class B: def __new__(cls) -> "C": return cast(C, None) class C(B): def __new__(cls) -> B: return cast(B, None) reveal_type...
X Description: new.py: from typing import cast class A: def __new__(cls) -> int: return 1 class B: def __new__(cls) -> "C": return cast(C, None) class C(B): def __new__(cls) -> B: return cast(B...
Opengraph URL: https://github.com/python/mypy/issues/19624
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`__new__` return types are not respected (reported as error and overwritten by the current class) unless the return type is a subclass of the current class","articleBody":"`new.py`:\n\n```python\nfrom typing import cast\n\nclass A:\n def __new__(cls) -> int:\n return 1\n\nclass B:\n def __new__(cls) -> \"C\":\n return cast(C, None)\n\nclass C(B):\n def __new__(cls) -> B:\n return cast(B, None)\n\nreveal_type(A()) # Expect int (builtin.int)\nreveal_type(B()) # Expect C (new.C)\nreveal_type(C()) # Expect B (new.B)\n```\n\nRun\n\n```shell\nmypy new.py --config-file=\n```\n\n(The `--config-file=` ignores configuration files)\n\nExpected:\n\n```\nnew.py:15: note: Revealed type is \"builtin.int\"\nnew.py:16: note: Revealed type is \"new.C\"\nnew.py:17: note: Revealed type is \"new.B\"\n```\n\nActual:\n\n```\nnew.py:4: error: Incompatible return type for \"__new__\" (returns \"int\", but must return a subtype of \"A\") [misc]\nnew.py:12: error: Incompatible return type for \"__new__\" (returns \"B\", but must return a subtype of \"C\") [misc]\nnew.py:15: note: Revealed type is \"new.A\"\nnew.py:16: note: Revealed type is \"new.C\"\nnew.py:17: note: Revealed type is \"new.C\"\nFound 2 errors in 1 file (checked 1 source file)\n```\n\n**Environment**\n\n- mypy 1.18.0+dev (compiled: no) (186515f66fc47d8afc0c9d43482265f295c83a08)\n - `python -m pip install -e . -C editable_mode=strict`\n- Python 3.13.2\n\n**Related issues**\n\nSame: #1020 #15182 #8330\nRelated: #14502 #12045 #9045 \n\n(Note: Some of these issues are noted as no longer reproducable in the comments. I didn't check their reproducability, but I am able to reproduce this one, which might mean a regression.)","author":{"url":"https://github.com/studyingegret","@type":"Person","name":"studyingegret"},"datePublished":"2025-08-09T06:22:34Z","url":"https://github.com/python/mypy/issues/19624"}
| 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:fba9335f-a17e-f43a-b6b4-aad587b5c487 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B6B8:34D7A7:297802:382485:6B00258B |
| html-safe-nonce | 20b3dbc813b037a7e0f4a4531daee4f78a992b1a3e7a5ebc014b8247036a6ea3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNkI4OjM0RDdBNzoyOTc4MDI6MzgyNDg1OjZCMDAyNThCIiwidmlzaXRvcl9pZCI6IjQxMjQzMDU3NTg2NzYzMjk4NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a048a14edc0920aafe5eedd1dd53414cdb3681db86ebacf59516084846a35388 |
| hovercard-subject-tag | issue:3305982889 |
| 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/19624/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f5c8629d575baffe2b6d93a68a5a545ae09a2ba85189ef992750e251748d918/python/mypy/issues/19624 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f5c8629d575baffe2b6d93a68a5a545ae09a2ba85189ef992750e251748d918/python/mypy/issues/19624 |
| og:image:alt | new.py: from typing import cast class A: def __new__(cls) -> int: return 1 class B: def __new__(cls) -> "C": return cast(C, None) class C(B): def __new__(cls) -> B: return cast(B, None) reveal_type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | studyingegret |
| 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