Title: Union[List[T], T] is not recognized by mypy · Issue #13792 · python/mypy · GitHub
Open Graph Title: Union[List[T], T] is not recognized by mypy · Issue #13792 · python/mypy
X Title: Union[List[T], T] is not recognized by mypy · Issue #13792 · python/mypy
Description: Bug Report Union[List[T], T] is not recognized by mypy To Reproduce from typing import List, Union, TypeVar T = TypeVar('T') def first_or_only(x: Union[List[T], T]) -> T: pass x1: int = first_or_only(1) x2: int = first_or_only([1, 2, 3])...
Open Graph Description: Bug Report Union[List[T], T] is not recognized by mypy To Reproduce from typing import List, Union, TypeVar T = TypeVar('T') def first_or_only(x: Union[List[T], T]) -> T: pass x1: int = first_or_on...
X Description: Bug Report Union[List[T], T] is not recognized by mypy To Reproduce from typing import List, Union, TypeVar T = TypeVar('T') def first_or_only(x: Union[List[T], T]) -> T: pass x1: int = ...
Opengraph URL: https://github.com/python/mypy/issues/13792
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Union[List[T], T] is not recognized by mypy","articleBody":"**Bug Report**\r\n\r\n`Union[List[T], T]` is not recognized by mypy\r\n\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import List, Union, TypeVar\r\n\r\nT = TypeVar('T')\r\n\r\n\r\ndef first_or_only(x: Union[List[T], T]) -> T:\r\n pass\r\n\r\nx1: int = first_or_only(1)\r\nx2: int = first_or_only([1, 2, 3])\r\nx3: tuple = first_or_only((1, 2, 3))\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n1. x1 should be inferenced as int\r\n2. x2 should be inferenced as a) perfect case: int b) correct case int or list[int]\r\n3. x3 should be inferenced as tuple[int]\r\n\r\nBoth `vs code` and `pycharm` inference as 1 + 2a + 3. \r\n\r\n**Actual Behavior**\r\n\r\n\r\nMypy output for the example above makes no sense to me:\r\n\r\n```\r\nerror: Argument 1 to \"first_or_only\" has incompatible type \"List[int]\"; expected \"List[\u003cnothing>]\"\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.971\r\n- Mypy command-line flags: just file\r\n- Mypy configuration options from `mypy.ini` (and other config files): none\r\n- Python version used: 3.10\r\n","author":{"url":"https://github.com/arogozhnikov","@type":"Person","name":"arogozhnikov"},"datePublished":"2022-10-03T02:03:44Z","url":"https://github.com/python/mypy/issues/13792"}
| 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:3df041d3-4866-6deb-f311-2f53ef8750ae |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C568:07D0:18A9D2D:2099DD0:6B002C77 |
| html-safe-nonce | 3fdd7e3ee84b8dab591bdc633443db30dfc2cd219d7be1d57fb422f2cb27f760 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTY4OjA3RDA6MThBOUQyRDoyMDk5REQwOjZCMDAyQzc3IiwidmlzaXRvcl9pZCI6IjkwNDQxMzA1ODA4MDI4NDE3MTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 715fbf1024427f5cd0b3a4097dd87c57dcb40d2873041847621710495e31194b |
| hovercard-subject-tag | issue:1394014995 |
| 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/13792/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1778270f0240daa4b4a051d7192f2b7756229867db34554263a1ec850db5e1e3/python/mypy/issues/13792 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1778270f0240daa4b4a051d7192f2b7756229867db34554263a1ec850db5e1e3/python/mypy/issues/13792 |
| og:image:alt | Bug Report Union[List[T], T] is not recognized by mypy To Reproduce from typing import List, Union, TypeVar T = TypeVar('T') def first_or_only(x: Union[List[T], T]) -> T: pass x1: int = first_or_on... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | arogozhnikov |
| 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