Title: Honor return type of `__new__` even if not a subclass · Issue #15182 · python/mypy · GitHub
Open Graph Title: Honor return type of `__new__` even if not a subclass · Issue #15182 · python/mypy
X Title: Honor return type of `__new__` even if not a subclass · Issue #15182 · python/mypy
Description: This follows gh-1020 which was closed by gh-7188. This arises because I am adding type hints to sympy (sympy/sympy#25103). In the following code we have a subclass whose __new__ method is only guaranteed to return objects that are types ...
Open Graph Description: This follows gh-1020 which was closed by gh-7188. This arises because I am adding type hints to sympy (sympy/sympy#25103). In the following code we have a subclass whose __new__ method is only guar...
X Description: This follows gh-1020 which was closed by gh-7188. This arises because I am adding type hints to sympy (sympy/sympy#25103). In the following code we have a subclass whose __new__ method is only guar...
Opengraph URL: https://github.com/python/mypy/issues/15182
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Honor return type of `__new__` even if not a subclass","articleBody":"This follows gh-1020 which was closed by gh-7188. This arises because I am adding type hints to sympy (https://github.com/sympy/sympy/pull/25103).\r\n\r\nIn the following code we have a subclass whose `__new__` method is only guaranteed to return objects that are types of the superclass:\r\n```python\r\nfrom __future__ import annotations\r\n\r\nclass A:\r\n def __new__(cls) -> A:\r\n return super().__new__(cls)\r\n\r\nclass B(A):\r\n def __new__(cls) -> A:\r\n return super().__new__(cls)\r\n\r\nreveal_type(B())\r\n```\r\nWith mypy this is rejected and the type of `B()` is inferred incorrectly but pyright accepts this and gets the inference for `B()` correct:\r\n```console\r\n$ mypy q.py\r\nq.py:8: error: Incompatible return type for \"__new__\" (returns \"A\", but must return a subtype of \"B\") [misc]\r\nq.py:11: note: Revealed type is \"q.B\"\r\nFound 1 error in 1 file (checked 1 source file)\r\n$ pyright q.py\r\n...\r\n ./q.py:11:13 - information: Type of \"B()\" is \"A\"\r\n0 errors, 0 warnings, 1 information \r\nCompleted in 0.955sec\r\n```\r\nThe fix for `__new__` in gh-7188 was:\r\n\r\n- If the return type is Any, ignore that and keep the class type as\r\nthe return type\r\n- Otherwise respect `__new__`'s return type\r\n- Produce an error if the return type is not a subtype of the class.\r\n\r\nThe issue here is about mixing the last two points. Here mypy produces an error but then does *not* respect `__new__`'s return type. The return type is not respected since mypy infers that it is of type `B` unlike pyright which infers type `A` as specified by the hint. I don't mind mypy reporting an error here but in context `-> A` is the accurate type hint and mypy should respect that because anything else is just not correct. I can add a type ignore to silence the mypy error but the inference will still be wrong in all downstream/user code and is also inconsistent with pyright.","author":{"url":"https://github.com/oscarbenjamin","@type":"Person","name":"oscarbenjamin"},"datePublished":"2023-05-04T15:07:40Z","url":"https://github.com/python/mypy/issues/15182"}
| 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:ade565b8-f957-f2f6-d2f1-fc420d40ee63 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B084:2DB972:2CA10E3:3AF389D:6B001D7B |
| html-safe-nonce | 5a86419c9850a5399508ac0f52e32511507954d1355ae1e6e2039772f93d2955 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDg0OjJEQjk3MjoyQ0ExMEUzOjNBRjM4OUQ6NkIwMDFEN0IiLCJ2aXNpdG9yX2lkIjoiNDU2NzY2NzkzMTE5ODAwNDYwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 93b024f665027321f3fb3b1cb4c782fd3abd3f01ea6c971fd1b49f5f9931a751 |
| hovercard-subject-tag | issue:1696212117 |
| 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/15182/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c5532d03adefc6f4d52c9d0aaf6fc3f9d1ec5b07c5f1eb7d1d7095af3b405992/python/mypy/issues/15182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c5532d03adefc6f4d52c9d0aaf6fc3f9d1ec5b07c5f1eb7d1d7095af3b405992/python/mypy/issues/15182 |
| og:image:alt | This follows gh-1020 which was closed by gh-7188. This arises because I am adding type hints to sympy (sympy/sympy#25103). In the following code we have a subclass whose __new__ method is only guar... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | oscarbenjamin |
| 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