Title: Type narrowing of a union type works incorrectly in presence of a list · Issue #11272 · python/mypy · GitHub
Open Graph Title: Type narrowing of a union type works incorrectly in presence of a list · Issue #11272 · python/mypy
X Title: Type narrowing of a union type works incorrectly in presence of a list · Issue #11272 · python/mypy
Description: Bug Report Minimum example: # Standard library modules from typing import List, Union Types = Union[str, List[str]] def f(x: Types) -> None: if isinstance(x, str): reveal_type(x) print("a string!") elif isinstance(x, list): reveal_type(x...
Open Graph Description: Bug Report Minimum example: # Standard library modules from typing import List, Union Types = Union[str, List[str]] def f(x: Types) -> None: if isinstance(x, str): reveal_type(x) print("a string!")...
X Description: Bug Report Minimum example: # Standard library modules from typing import List, Union Types = Union[str, List[str]] def f(x: Types) -> None: if isinstance(x, str): reveal_type(x) print("a s...
Opengraph URL: https://github.com/python/mypy/issues/11272
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing of a union type works incorrectly in presence of a list","articleBody":"**Bug Report**\r\n\r\nMinimum example:\r\n```\r\n# Standard library modules\r\nfrom typing import List, Union\r\n\r\nTypes = Union[str, List[str]]\r\n\r\n\r\ndef f(x: Types) -> None:\r\n if isinstance(x, str):\r\n reveal_type(x)\r\n print(\"a string!\")\r\n elif isinstance(x, list):\r\n reveal_type(x)\r\n print(\"a list!\")\r\n else:\r\n reveal_type(x)\r\n print(\"unreachable code\")\r\n```\r\n\r\nThe output of `mypy test.py` is:\r\n```\r\ntest.py:9: note: Revealed type is \"builtins.str\"\r\ntest.py:12: note: Revealed type is \"builtins.list[builtins.str]\"\r\ntest.py:15: note: Revealed type is \"builtins.list[builtins.str]\"\r\n```\r\n\r\nThe detected type for line 15 is actually incorrect, the code is (or better should be) unreachable and `List[str]` has already been dealt with in the second branch.\r\n\r\n**To Reproduce**\r\n\r\n1. Save the above code to `test.py`\r\n2. Run `mypy test.py`\r\n\r\n**Expected Behavior**\r\n\r\nI expect that it correctly recognises the code as unreachable.\r\n\r\n**Actual Behavior**\r\n\r\nmypy thinks that the type in the branch is reachable with type `List[str]`.\r\n\r\nAn interesting variation of the above is using `Types = Union[str, List[str], List[int]]`. Then the output is:\r\n```\r\ntest.py:9: note: Revealed type is \"builtins.str\"\r\ntest.py:12: note: Revealed type is \"Union[builtins.list[builtins.str], builtins.list[builtins.int]]\"\r\ntest.py:15: note: Revealed type is \"\u003cnothing>\"\r\n```\r\nwhich is correct.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: mypy 0.910\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: Python 3.9.7\r\n- Operating system and version: MacOS 11.5.2\r\n","author":{"url":"https://github.com/yasinzaehringer-paradime","@type":"Person","name":"yasinzaehringer-paradime"},"datePublished":"2021-10-05T16:09:12Z","url":"https://github.com/python/mypy/issues/11272"}
| 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:242305e0-eb3b-b15b-dcfc-90d88ea2b9aa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB72:2046A4:E36994:130486B:6B005601 |
| html-safe-nonce | 0fee6a18afe85f8d4997372701ab77c39fbe217d4bcdc56294e7d5985873a4c5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjcyOjIwNDZBNDpFMzY5OTQ6MTMwNDg2Qjo2QjAwNTYwMSIsInZpc2l0b3JfaWQiOiI4MzAxMzAzODUzNjc3MDQxMTUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 78ecff72e350c2bb6e126106824d3d5d0bbca39fa9a1a1b26aede3ce2ddb8972 |
| hovercard-subject-tag | issue:1016512176 |
| 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/11272/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bdf0724eb493a1b66315e1e02eda7bac062ef2ddf641640bd6571a73476f9c04/python/mypy/issues/11272 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bdf0724eb493a1b66315e1e02eda7bac062ef2ddf641640bd6571a73476f9c04/python/mypy/issues/11272 |
| og:image:alt | Bug Report Minimum example: # Standard library modules from typing import List, Union Types = Union[str, List[str]] def f(x: Types) -> None: if isinstance(x, str): reveal_type(x) print("a string!")... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yasinzaehringer-paradime |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width