Title: Tagged unions don't work with Enums · Issue #12432 · python/mypy · GitHub
Open Graph Title: Tagged unions don't work with Enums · Issue #12432 · python/mypy
X Title: Tagged unions don't work with Enums · Issue #12432 · python/mypy
Description: Bug Report With the example below, the second if doesn't properly narrow the type, it works fine if the Literal['b'] instead of the enum version. To Reproduce from queue import Queue from typing import Literal, TypedDict, Union from enum...
Open Graph Description: Bug Report With the example below, the second if doesn't properly narrow the type, it works fine if the Literal['b'] instead of the enum version. To Reproduce from queue import Queue from typing im...
X Description: Bug Report With the example below, the second if doesn't properly narrow the type, it works fine if the Literal['b'] instead of the enum version. To Reproduce from queue import Queue fr...
Opengraph URL: https://github.com/python/mypy/issues/12432
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Tagged unions don't work with Enums","articleBody":"**Bug Report**\r\n\r\nWith the example below, the second `if` doesn't properly narrow the type, it works fine if the `Literal['b']` instead of the enum version.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom queue import Queue\r\nfrom typing import Literal, TypedDict, Union\r\nfrom enum import Enum, unique\r\n\r\n@unique\r\nclass Tag(Enum):\r\n a = 'a'\r\n b = 'b'\r\n\r\nclass OpA(TypedDict):\r\n tag: Literal[Tag.a]\r\n a: str\r\n\r\nclass OpB(TypedDict):\r\n tag: Literal[Tag.b]\r\n b: str\r\n\r\nOps = Union[OpA, OpB]\r\n\r\ndef do(op: Ops):\r\n if op['tag'] == Tag.a:\r\n reveal_type(op)\r\n if op['tag'] == Tag.b:\r\n reveal_type(op)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n```python\r\n if op['tag'] == Tag.b:\r\n reveal_type(op)\r\n```\r\n\r\nShould report:\r\n\r\n```\r\nt.py:24: note: Revealed type is \"TypedDict('t.OpB', {'tag': Literal[t.Tag.b], 'b': builtins.str})\"\r\n```\r\n\r\n**Actual Behavior**\r\n\r\nReported type is:\r\n\r\n```\r\nt.py:24: note: Revealed type is \"Union[TypedDict('t.OpA', {'tag': Literal[t.Tag.a], 'a': builtins.str}), TypedDict('t.OpB', {'tag': Literal[t.Tag.b], 'b': builtins.str})]\"\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version(s) used: `mypy 0.910`, `mypy 0.941`, `mypy 0.950+dev.fd8a15e1abdd2bb4f75c3a6319ebfba233e1fe8e (compiled: no)`\r\n- Mypy command-line flags: `mypy t.py`\r\n- Mypy configuration options from `mypy.ini` (and other config files): `nothing`\r\n- Python version used: `Python 3.10.2`\r\n- Operating system and version: `Fedora Linux 35`","author":{"url":"https://github.com/hackaugusto","@type":"Person","name":"hackaugusto"},"datePublished":"2022-03-23T14:55:59Z","url":"https://github.com/python/mypy/issues/12432"}
| 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:550b201e-63f1-f231-6210-536aaab24841 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B988:3E1398:AA569A:E4171F:6B00488A |
| html-safe-nonce | 4a02b32fa2274213ebc82e044f1761d82f71b5acdd959ff94a03a6defa0b3929 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTg4OjNFMTM5ODpBQTU2OUE6RTQxNzFGOjZCMDA0ODhBIiwidmlzaXRvcl9pZCI6Ijc1NDM5ODM4MDY2NTU1MTQ3NjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b3aaca4a82348dbfbf7c2bd1ed9251f15a1921f847d17cf4e950564678c933ad |
| hovercard-subject-tag | issue:1178238284 |
| 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/12432/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f682f56613d17bd7f11b237055160bdc169313bd7c799b6e790d8f85fd1c1874/python/mypy/issues/12432 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f682f56613d17bd7f11b237055160bdc169313bd7c799b6e790d8f85fd1c1874/python/mypy/issues/12432 |
| og:image:alt | Bug Report With the example below, the second if doesn't properly narrow the type, it works fine if the Literal['b'] instead of the enum version. To Reproduce from queue import Queue from typing im... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hackaugusto |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width