Title: Nested tagged unions stopped working · Issue #10764 · python/mypy · GitHub
Open Graph Title: Nested tagged unions stopped working · Issue #10764 · python/mypy
X Title: Nested tagged unions stopped working · Issue #10764 · python/mypy
Description: Bug Report Starting with mypy 0.900 tagged unions don't work anymore if they are nested. To Reproduce from typing import Literal, Union from enum import Enum class ModelType(str, Enum): Simple1 = "simple1" Simple2 = "simple2" Complex1 = ...
Open Graph Description: Bug Report Starting with mypy 0.900 tagged unions don't work anymore if they are nested. To Reproduce from typing import Literal, Union from enum import Enum class ModelType(str, Enum): Simple1 = "...
X Description: Bug Report Starting with mypy 0.900 tagged unions don't work anymore if they are nested. To Reproduce from typing import Literal, Union from enum import Enum class ModelType(str, Enum): Simple1...
Opengraph URL: https://github.com/python/mypy/issues/10764
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Nested tagged unions stopped working","articleBody":"\u003c!--\r\n If you're new to mypy and you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form\r\n instead.\r\n-->\r\n\r\n**Bug Report**\r\n\r\n\u003c!--\r\nNote: If the problem you are reporting is about a specific library function, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n-->\r\n\r\nStarting with mypy 0.900 tagged unions don't work anymore if they are nested.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import Literal, Union\r\nfrom enum import Enum\r\n\r\nclass ModelType(str, Enum):\r\n Simple1 = \"simple1\"\r\n Simple2 = \"simple2\"\r\n Complex1 = \"complex1\"\r\n Complex2 = \"complex2\"\r\n\r\nclass BaseModel:\r\n type: ModelType\r\n \r\nclass BaseSimpleModel(BaseModel):\r\n type: Literal[ModelType.Simple1, ModelType.Simple2]\r\n simple: str\r\n \r\nclass Simple1Model(BaseSimpleModel):\r\n type: Literal[ModelType.Simple1]\r\n \r\nclass Simple2Model(BaseSimpleModel):\r\n type: Literal[ModelType.Simple2]\r\n \r\nSimpleModel = Union[Simple1Model, Simple2Model]\r\n\r\nclass BaseComplexModel(BaseModel):\r\n type: Literal[ModelType.Complex1, ModelType.Complex2]\r\n complex: str\r\n\r\nclass Complex1Model(BaseComplexModel):\r\n type: Literal[ModelType.Complex1]\r\n \r\nclass Complex2Model(BaseComplexModel):\r\n type: Literal[ModelType.Complex2]\r\n \r\nComplexModel = Union[Complex1Model, Complex2Model]\r\n \r\nModel = Union[SimpleModel, ComplexModel]\r\n\r\n\r\nmodel: Model = Simple1Model()\r\n\r\nif model.type == ModelType.Simple1 or model.type == ModelType.Simple2:\r\n reveal_type(model)\r\n reveal_type(model.simple)\r\nelse:\r\n reveal_type(model)\r\n reveal_type(model.complex)\r\n```\r\n\r\nType-check with mypy >= 0.900 and it won't pass, try with mypy 0.812 for example, and it will pass.\r\n\r\nI've also prepared it on mypy-play.\r\n\r\nWorking (mypy 0.812):\r\nhttps://mypy-play.net/?mypy=0.812&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead\r\nBroken (mypy 0.910):\r\nhttps://mypy-play.net/?mypy=0.910&python=3.9&gist=b15073e2bdc681689f2e6f9bc8556ead\r\n\r\n**Expected Behavior**\r\n\r\n```\r\nmain.py:43: note: Revealed type is 'Union[main.Simple1Model, main.Simple2Model]'\r\nmain.py:44: note: Revealed type is 'builtins.str'\r\nmain.py:46: note: Revealed type is 'Union[main.Complex1Model, main.Complex2Model]'\r\nmain.py:47: note: Revealed type is 'builtins.str'\r\n```\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:43: note: Revealed type is \"Union[main.Simple1Model, main.Simple2Model, main.Complex1Model, main.Complex2Model]\"\r\nmain.py:44: error: Item \"Complex1Model\" of \"Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]\" has no attribute \"simple\"\r\nmain.py:44: error: Item \"Complex2Model\" of \"Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]\" has no attribute \"simple\"\r\nmain.py:44: note: Revealed type is \"Union[builtins.str, Any]\"\r\nmain.py:46: note: Revealed type is \"Union[main.Simple1Model, main.Simple2Model, main.Complex1Model, main.Complex2Model]\"\r\nmain.py:47: error: Item \"Simple1Model\" of \"Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]\" has no attribute \"complex\"\r\nmain.py:47: error: Item \"Simple2Model\" of \"Union[Union[Simple1Model, Simple2Model], Union[Complex1Model, Complex2Model]]\" has no attribute \"complex\"\r\nmain.py:47: note: Revealed type is \"Union[Any, builtins.str]\"\r\n```\r\n\r\nAs you can see mypy isn't able to correctly nail down the union members anymore when unions are nested.\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: 0.912\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- Operating system and version: macOS\r\n\r\n\u003c!--\r\nYou can freely edit this text, please remove all the lines\r\nyou believe are unnecessary.\r\n-->\r\n","author":{"url":"https://github.com/levrik","@type":"Person","name":"levrik"},"datePublished":"2021-07-05T11:14:32Z","url":"https://github.com/python/mypy/issues/10764"}
| 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:f1da15a3-71e0-f46d-6d23-3a70e3e7ed9c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB2E:B1603:30F1C2C:40D20B2:6B002F38 |
| html-safe-nonce | 979abb5125e0fea5ee6e601ef2530b71284fecfd0719cfcd8a4b3dd642bdc283 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjJFOkIxNjAzOjMwRjFDMkM6NDBEMjBCMjo2QjAwMkYzOCIsInZpc2l0b3JfaWQiOiIzODczMjY0MTI1NjU2MTgyNTg0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 05105c2770d43568d37288b7e6ad1370bdd29ea35130bb022452dd9411a443bd |
| hovercard-subject-tag | issue:936983094 |
| 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/10764/issue_layout |
| twitter:image | https://opengraph.githubassets.com/459b12dd2e14889a49ba1c67937f476da548ca05902dae26e373ecf053b6ed1a/python/mypy/issues/10764 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/459b12dd2e14889a49ba1c67937f476da548ca05902dae26e373ecf053b6ed1a/python/mypy/issues/10764 |
| og:image:alt | Bug Report Starting with mypy 0.900 tagged unions don't work anymore if they are nested. To Reproduce from typing import Literal, Union from enum import Enum class ModelType(str, Enum): Simple1 = "... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | levrik |
| 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