Title: Argument type `Union[T, List[T]]` fails to infer constaints · Issue #11149 · python/mypy · GitHub
Open Graph Title: Argument type `Union[T, List[T]]` fails to infer constaints · Issue #11149 · python/mypy
X Title: Argument type `Union[T, List[T]]` fails to infer constaints · Issue #11149 · python/mypy
Description: While working on #11128 I got hit by this. This code does not type check: from typing import TypeVar, Union, List T1 = TypeVar("T1") def promote_list(item: Union[T1, List[T1]]) -> List[T1]: ... exprs: List[int] reveal_type(promote_list(e...
Open Graph Description: While working on #11128 I got hit by this. This code does not type check: from typing import TypeVar, Union, List T1 = TypeVar("T1") def promote_list(item: Union[T1, List[T1]]) -> List[T1]: ... exp...
X Description: While working on #11128 I got hit by this. This code does not type check: from typing import TypeVar, Union, List T1 = TypeVar("T1") def promote_list(item: Union[T1, List[T1]]) -> List...
Opengraph URL: https://github.com/python/mypy/issues/11149
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Argument type `Union[T, List[T]]` fails to infer constaints","articleBody":"While working on #11128 I got hit by this.\r\n\r\nThis code does not type check:\r\n\r\n```python\r\nfrom typing import TypeVar, Union, List\r\n\r\nT1 = TypeVar(\"T1\")\r\n\r\ndef promote_list(item: Union[T1, List[T1]]) -> List[T1]:\r\n ...\r\n\r\nexprs: List[int]\r\nreveal_type(promote_list(exprs)) # error\r\n# ex.py:9: note: Revealed type is \"builtins.list[\u003cnothing>]\"\r\n# ex.py:9: error: Argument 1 to \"promote_list\" has incompatible type \"List[int]\"; expected \"List[\u003cnothing>]\"\r\n\r\nreveal_type(promote_list(1)) # ok\r\n# Revealed type is \"builtins.list[builtins.int*]\"\r\n```\r\n\r\nWhich is not what I expect. For example, TypeScript solves this properly:\r\n\r\n```ts\r\nfunction protomoteList\u003cT>(input: T | T[]): T[] {\r\n return []\r\n}\r\n\r\nfunction test(input: number[]) {}\r\n\r\ntest(protomoteList(1)) # ok\r\n```\r\n\r\nLink: https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABABwE5ynAthgpgGRgGcoAeAFQD4AKGMZEKALkXMQB9WBtAXQEoW5XogDeAWABQiaYlS4oIVEl6SAvpMmhIsBIii4SteoxZgQWAEa5UvPqPUTJ+w2gzY8hQwEY+fSUA\r\n\r\n## Cause\r\n\r\nIt happens somewhere here: https://github.com/python/mypy/blob/b3ff2a683bd41a176c597d3c3ba65f6a31f805c5/mypy/constraints.py#L168-L171\r\n\r\nI will try to solve this, but I know that this is going to be complicated.","author":{"url":"https://github.com/sobolevn","@type":"Person","name":"sobolevn"},"datePublished":"2021-09-20T14:25:16Z","url":"https://github.com/python/mypy/issues/11149"}
| 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:eb422099-389e-6fd3-5d48-f08e21789e27 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F8E:1EF2:9D6102:DAA335:6B002A9B |
| html-safe-nonce | fe617add9e22e617e9dc1aa5e0908dcbdb0248ea4843003dd5f50fd69d4f0dc5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjhFOjFFRjI6OUQ2MTAyOkRBQTMzNTo2QjAwMkE5QiIsInZpc2l0b3JfaWQiOiIxMTQ2NzQ3NjExMTczMTA0Mjg0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | beb706d5279a37f233e6c832cc7dac8be9f7f018a565e411f8306c9766856216 |
| hovercard-subject-tag | issue:1001035201 |
| 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/11149/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8e4f903b0f6787435a6bac4f0ff76f96723f5b8f324880c65a94ea4075438f8d/python/mypy/issues/11149 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8e4f903b0f6787435a6bac4f0ff76f96723f5b8f324880c65a94ea4075438f8d/python/mypy/issues/11149 |
| og:image:alt | While working on #11128 I got hit by this. This code does not type check: from typing import TypeVar, Union, List T1 = TypeVar("T1") def promote_list(item: Union[T1, List[T1]]) -> List[T1]: ... exp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sobolevn |
| 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