Title: Generic and __new__ with returns to a subclass produces incorrect Union inference · Issue #14502 · python/mypy · GitHub
Open Graph Title: Generic and __new__ with returns to a subclass produces incorrect Union inference · Issue #14502 · python/mypy
X Title: Generic and __new__ with returns to a subclass produces incorrect Union inference · Issue #14502 · python/mypy
Description: Bug Report There seems to be an odd interaction between Union, __new__() and Generic causing incorrect inferences to occur. Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516 Might be related to both #14009 and #140...
Open Graph Description: Bug Report There seems to be an odd interaction between Union, __new__() and Generic causing incorrect inferences to occur. Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516 ...
X Description: Bug Report There seems to be an odd interaction between Union, __new__() and Generic causing incorrect inferences to occur. Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516 ...
Opengraph URL: https://github.com/python/mypy/issues/14502
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic and __new__ with returns to a subclass produces incorrect Union inference","articleBody":"\r\n**Bug Report**\r\n\r\nThere seems to be an odd interaction between `Union`, `__new__()` and `Generic` causing incorrect inferences to occur.\r\n\r\nCame up in a reported issue in pandas-stubs: https://github.com/pandas-dev/pandas-stubs/issues/516\r\n\r\nMight be related to both #14009 and #14061\r\n\r\n```python\r\nfrom __future__ import annotations\r\n\r\nfrom typing import TypeVar, Generic, Union, overload\r\n\r\nT = TypeVar(\"T\", int, str)\r\n\r\n\r\nclass Series(Generic[T]):\r\n @overload\r\n def __new__(cls, dtype: int) -> IntSeries:\r\n ...\r\n\r\n @overload\r\n def __new__(cls, dtype: type[T]) -> Series[T]:\r\n ...\r\n\r\n @overload\r\n def __new__(cls, dtype=...) -> Series:\r\n ...\r\n\r\n def __new__(cls, dtype=...) -> Series:\r\n cls.dtype = dtype\r\n return object.__new__(cls)\r\n\r\n ...\r\n\r\n\r\nclass IntSeries(Series[int]):\r\n ...\r\n\r\n\r\nclass Index:\r\n @overload\r\n def __new__(cls, dtype: int) -> IntIndex:\r\n ...\r\n\r\n @overload\r\n def __new__(cls, dtype=...) -> Index:\r\n ...\r\n\r\n def __new__(cls, dtype=...) -> Index:\r\n cls.dtype = dtype\r\n return object.__new__(cls)\r\n\r\n\r\nclass IntIndex(Index):\r\n ...\r\n\r\n\r\ndef foo(x: Union[Series, int, Index]):\r\n if isinstance(x, (Series, Index)):\r\n reveal_type(x)\r\n else:\r\n reveal_type(x)\r\n```\r\n\r\n**Expected Behavior**\r\n```text\r\ngenunion.py:52: note: Revealed type is \"Union[genunion.Series[Any], genunion.Index]\"\r\ngenunion.py:54: note: Revealed type is \"int\"\r\n```\r\n\r\npyright handles this correctly\r\n\r\n**Actual Behavior**\r\n\r\n```text\r\ngenunion.py:52: note: Revealed type is \"Union[genunion.IntSeries, genunion.IntIndex]\"\r\ngenunion.py:54: note: Revealed type is \"Union[genunion.Series[Any], builtins.int, genunion.Index]\"\r\n```\r\n\r\n**Your Environment**\r\n\r\n\r\n- Mypy version used: 0.991\r\n- Mypy command-line flags: none\r\n- Mypy configuration options from `mypy.ini` (and other config files): none\r\n- Python version used: 3.9\r\n\r\n","author":{"url":"https://github.com/Dr-Irv","@type":"Person","name":"Dr-Irv"},"datePublished":"2023-01-22T01:22:41Z","url":"https://github.com/python/mypy/issues/14502"}
| 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:e2258221-7c96-82ed-9777-b930190cfbf8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8FA:1CF166:22B94D2:2F3C3C0:6B002B91 |
| html-safe-nonce | 8e212bdf78b47c10cd79ad43b7ceb6e44f92684c10200652bd7610d508496474 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEZBOjFDRjE2NjoyMkI5NEQyOjJGM0MzQzA6NkIwMDJCOTEiLCJ2aXNpdG9yX2lkIjoiMTUwNTcyNjMwNzgyNzE5ODg2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 967adae6ec1870da76cff5250765897af5a930b1d516c0b7ebfeeb311b737470 |
| hovercard-subject-tag | issue:1551971310 |
| 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/14502/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0de4e4e46b606e510089407c73f05708ef8670a964a0b5ca3c23679d9ef536d2/python/mypy/issues/14502 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0de4e4e46b606e510089407c73f05708ef8670a964a0b5ca3c23679d9ef536d2/python/mypy/issues/14502 |
| og:image:alt | Bug Report There seems to be an odd interaction between Union, __new__() and Generic causing incorrect inferences to occur. Came up in a reported issue in pandas-stubs: pandas-dev/pandas-stubs#516 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Dr-Irv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width