Title: [1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type · Issue #21187 · python/mypy · GitHub
Open Graph Title: [1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type · Issue #21187 · python/mypy
X Title: [1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type · Issue #21187 · python/mypy
Description: Bug Report Under specific circumstances, changing the order of case-statements in a match-statement influences mypy's type narrowing, leading to flaky assert_never errors. Specifically, this happens you narrow enum values to specific enu...
Open Graph Description: Bug Report Under specific circumstances, changing the order of case-statements in a match-statement influences mypy's type narrowing, leading to flaky assert_never errors. Specifically, this happen...
X Description: Bug Report Under specific circumstances, changing the order of case-statements in a match-statement influences mypy's type narrowing, leading to flaky assert_never errors. Specifically, this ha...
Opengraph URL: https://github.com/python/mypy/issues/21187
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type","articleBody":"## Bug Report\n\nUnder specific circumstances, changing the order of `case`-statements in a `match`-statement influences mypy's type narrowing, leading to flaky `assert_never` errors.\n\nSpecifically, this happens you narrow enum values to specific enum values using `Literal` and combine that narrowed type with other types in a union.\n\n(I've not done an exhaustive search, but I've only seen this with `Literal[SomeEnum.SPECIFIC_VALUE]`.)\n\n### This bug does not occur with mypy==1.19\n\nI've tried to reproduce this bug with Mypy 1.19, but it only occurs with Mypy 1.20. (We ran into this after upgrading Mypy.)\n\n## To Reproduce\n\nIn the script below, the only different between the `dummy_class_then_enum` and `enum_then_dummy_class` is the order of the `case`-statements within the `match` statement.\n\nIn both instances, all possible value types are handled, but `mypy` will throw an error on the `assert_never` in the `enum_then_dummy_class` function.\n\n\n```python\nimport enum\nfrom typing import Literal, assert_never\n\n\nclass DummyClass:\n \"\"\"A dummy class.\"\"\"\n\n\nclass MyEnum(enum.StrEnum):\n \"\"\"A dummy enum class.\"\"\"\n\n RELEVANT = enum.auto()\n IGNORED = enum.auto()\n\n\ntype MyUnionType = DummyClass | Literal[MyEnum.RELEVANT]\n\n\ndef dummy_class_then_enum(arg: MyUnionType) -\u003e str:\n match arg:\n case DummyClass():\n return \"dummy class\"\n case MyEnum.RELEVANT:\n return \"relevant\"\n case _ as unreachable:\n assert_never(unreachable)\n\n\ndef enum_then_dummy_class(arg: MyUnionType) -\u003e str:\n match arg:\n case MyEnum.RELEVANT:\n return \"relevant\"\n case DummyClass():\n return \"dummy class\"\n case _ as unreachable:\n assert_never(unreachable)\n\n\n\n```\n\n### Expected Behavior\n\nMypy reports no errors (or errors for both functions).\n\n### Actual Behavior\n\nWe only get an error for the second function, not the first.\n\n```\n(mypy-demo) sebastiaan@python MypyDemo % mypy bug_demo.py\nbug_demo.py:36: error: Argument 1 to \"assert_never\" has incompatible type \"Literal[MyEnum.RELEVANT]\"; expected \"Never\" [arg-type]\nFound 1 error in 1 file (checked 1 source file)\n```\n\n## Your Environment\n\n- Mypy version used: `1.20`\n- Mypy command-line flags: None\n- Mypy configuration options from `mypy.ini` (and other config files): None\n- Python version used: `3.14.3`\n","author":{"url":"https://github.com/SebastiaanZ","@type":"Person","name":"SebastiaanZ"},"datePublished":"2026-04-08T13:45:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/21187/mypy/issues/21187"}
| 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:17ce0708-1859-476e-b6ad-6f13c38fe909 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D610:3E9CB8:FF1F45:17221E4:6A57A9DA |
| html-safe-nonce | ff4e9de22f1e7975245c119c7d33d6618e4ecf32e9803dfeeb8670bad4afc4d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjEwOjNFOUNCODpGRjFGNDU6MTcyMjFFNDo2QTU3QTlEQSIsInZpc2l0b3JfaWQiOiI3NjUxOTU4MjMyMTk4OTE2NTcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 65ad33379ee599a932c80920f6569615a8ae24958fd3f2db6c27868cc0b8df52 |
| hovercard-subject-tag | issue:4224998729 |
| 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/21187/issue_layout |
| twitter:image | https://opengraph.githubassets.com/822da0270612408c391d8980ad6c37021d2a893ed4df38ca90b84c0dbecc5cb2/python/mypy/issues/21187 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/822da0270612408c391d8980ad6c37021d2a893ed4df38ca90b84c0dbecc5cb2/python/mypy/issues/21187 |
| og:image:alt | Bug Report Under specific circumstances, changing the order of case-statements in a match-statement influences mypy's type narrowing, leading to flaky assert_never errors. Specifically, this happen... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SebastiaanZ |
| hostname | github.com |
| expected-hostname | github.com |
| None | 017a07c29bdd8a37b5907fc9a689338deb7103b5af7f56714193b2700229b3df |
| 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 | 4cd16a676f89d1cedb932671a5c2d20b8e60f86a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width