Title: Internal error with TypeGuard and Literal-based union narrowing using StrEnum · Issue #18895 · python/mypy · GitHub
Open Graph Title: Internal error with TypeGuard and Literal-based union narrowing using StrEnum · Issue #18895 · python/mypy
X Title: Internal error with TypeGuard and Literal-based union narrowing using StrEnum · Issue #18895 · python/mypy
Description: Crash Report When using TypeGuard to narrow a Literal-based union from a StrEnum, mypy raises an internal error. I found a workaround, but I prefer my first implementation as it is more concise. Traceback main.py: error: INTERNAL ERROR -...
Open Graph Description: Crash Report When using TypeGuard to narrow a Literal-based union from a StrEnum, mypy raises an internal error. I found a workaround, but I prefer my first implementation as it is more concise. Tr...
X Description: Crash Report When using TypeGuard to narrow a Literal-based union from a StrEnum, mypy raises an internal error. I found a workaround, but I prefer my first implementation as it is more concise. Tr...
Opengraph URL: https://github.com/python/mypy/issues/18895
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Internal error with TypeGuard and Literal-based union narrowing using StrEnum","articleBody":"\u003c!--\n Use this form only if mypy reports an \"INTERNAL ERROR\" and/or gives a traceback.\n Please include the traceback and all other messages below (use `mypy --show-traceback`).\n-->\n\n**Crash Report**\n\nWhen using TypeGuard to narrow a Literal-based union from a StrEnum, mypy raises an internal error.\nI found a workaround, but I prefer my first implementation as it is more concise.\n\n**Traceback**\n\n```\nmain.py: error: INTERNAL ERROR -- Please try using mypy master on GitHub:\nhttps://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build\nIf this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues\nversion: 1.10.0\nmain.py: : note: please use --show-traceback to print a traceback when reporting a bug\n```\n\n**To Reproduce**\n\n```\nfrom typing import TypeGuard, Literal, TypeAlias\nfrom enum import StrEnum\n\nclass Model(StrEnum):\n MODEL_A1 = 'model_a1'\n MODEL_A2 = 'model_a2'\n MODEL_B = 'model_b'\n\nMODEL_A: TypeAlias = Literal[Model.MODEL_A1, Model.MODEL_A2]\nMODEL_B: TypeAlias = Literal[Model.MODEL_B]\n\ndef is_model_a(model: str) -> TypeGuard[MODEL_A]:\n return model in (Model.MODEL_A1, Model.MODEL_A2)\n\ndef is_model_b(model: str) -> TypeGuard[MODEL_B]:\n return model == Model.MODEL_B\n\ndef process_model(model: MODEL_A | MODEL_B) -> int:\n return 42\n\n# Works\ndef handle_1(model: Model) -> int:\n if is_model_a(model):\n return process_model(model)\n if is_model_b(model):\n return process_model(model)\n return 0\n\n# Crashes\ndef handle_2(model: Model) -> int:\n if is_model_a(model) or is_model_b(model): # ❌ This line causes a crash\n return process_model(model)\n return 0\n\n# Crashes\ndef handle_3(model: Model) -> int:\n match model:\n case m if is_model_a(m) or is_model_b(m): # ❌ Also crashes\n return process_model(m)\n case _:\n return 0\n\n```\n\n**Your Environment**\n\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\n\n- Mypy version used: 1.15.0\n- Mypy command-line flags: \n- Mypy configuration options from `mypy.ini` (and other config files):\n- Python version used: 3.12\n- Operating system and version: Linux 6.13.8\n\n\u003c!--\nYou can freely edit this text, please remove all the lines\nyou believe are unnecessary.\n-->\n","author":{"url":"https://github.com/frilox042","@type":"Person","name":"frilox042"},"datePublished":"2025-04-07T17:23:32Z","url":"https://github.com/python/mypy/issues/18895"}
| 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:342640b6-6096-9f69-d3f1-3995aeb836df |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A506:33DDEF:200D0DD:2BDDAFF:6B00287D |
| html-safe-nonce | c51eee1c52764ad1df949a4fe20ee2fe38da38618a8abe7d5d24f49d0e3fb0cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTA2OjMzRERFRjoyMDBEMEREOjJCRERBRkY6NkIwMDI4N0QiLCJ2aXNpdG9yX2lkIjoiNTY5NjA5NjM5MjY2MTk2OTAyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 39ecc7b88f2a949d8efdc068fcc77fdcaed741062ee2528449c3d310d8abcfb9 |
| hovercard-subject-tag | issue:2977520253 |
| 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/18895/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8dab7726def46981b79204f4859c2fbbc251cf79ae0ade69639c573f2ee99075/python/mypy/issues/18895 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8dab7726def46981b79204f4859c2fbbc251cf79ae0ade69639c573f2ee99075/python/mypy/issues/18895 |
| og:image:alt | Crash Report When using TypeGuard to narrow a Literal-based union from a StrEnum, mypy raises an internal error. I found a workaround, but I prefer my first implementation as it is more concise. Tr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | frilox042 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width