Title: Sentinel and enum type merge issues with generic · Issue #22334 · python/mypy · GitHub
Open Graph Title: Sentinel and enum type merge issues with generic · Issue #22334 · python/mypy
X Title: Sentinel and enum type merge issues with generic · Issue #22334 · python/mypy
Description: from typing_extensions import sentinel Undefined = sentinel("Undefined") def none_if_undefined[_T](value: _T | Undefined) -> _T | None: return None if value is Undefined else value def func() -> None: device_id: str | Undefined | None = ...
Open Graph Description: from typing_extensions import sentinel Undefined = sentinel("Undefined") def none_if_undefined[_T](value: _T | Undefined) -> _T | None: return None if value is Undefined else value def func() -> No...
X Description: from typing_extensions import sentinel Undefined = sentinel("Undefined") def none_if_undefined[_T](value: _T | Undefined) -> _T | None: return None if value is Undefined else value def...
Opengraph URL: https://github.com/python/mypy/issues/22334
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sentinel and enum type merge issues with generic","articleBody":"```py\nfrom typing_extensions import sentinel\n\nUndefined = sentinel(\"Undefined\")\n\ndef none_if_undefined[_T](value: _T | Undefined) -> _T | None:\n return None if value is Undefined else value\n\ndef func() -> None:\n device_id: str | Undefined | None = Undefined\n device_id = none_if_undefined(device_id) # \u003c-- error\n reveal_type(device_id)\n```\n```py\ntest.py:10: error: Argument 1 to \"none_if_undefined\" has incompatible type \"str | Undefined | None\"; expected \"str | Undefined\" [arg-type]\ntest.py:11: note: Revealed type is \"str | Undefined | None\"\n```\n\nHowever, assigning the result from `none_if_undefined` to a temporary variable first works as expected.\n```py\ndef func() -> None:\n device_id: str | Undefined | None = Undefined\n x = none_if_undefined(device_id)\n reveal_type(x)\n device_id = x\n reveal_type(device_id)\n```\n```py\ntest.py:11: note: Revealed type is \"str | None\"\ntest.py:13: note: Revealed type is \"str | None\"\n```\n\n--\nA similar issues exists for enums\n```py\nfrom enum import Enum\nfrom typing import Literal\n\nclass E(Enum):\n SPECIAL = 1\n\ndef none_if_special[_T](value: _T | Literal[E.SPECIAL]) -> _T | None:\n return None if value is E.SPECIAL else value\n\ndef func() -> None:\n device_id: str | Literal[E.SPECIAL] | None = E.SPECIAL\n device_id = none_if_special(device_id) # \u003c-- error\n reveal_type(device_id)\n```\n```py\ntest.py:12: error: Argument 1 to \"none_if_special\" has incompatible type \"str | Literal[E.SPECIAL] | None\"; expected \"str | Literal[E.SPECIAL]\" [arg-type]\ntest.py:13: note: Revealed type is \"str | Literal[test7.E.SPECIAL] | None\"\n```\n\n--\nFor completeness the related example with just `int` - that works fine -- no errors.\n```py\ndef none_if_int[_T](value: _T | int) -> _T | None:\n return None if isinstance(value, int) else value\n\ndef func() -> None:\n device_id: str | int | None = 42\n device_id = none_if_int(device_id)\n reveal_type(device_id)\n```","author":{"url":"https://github.com/cdce8p","@type":"Person","name":"cdce8p"},"datePublished":"2026-10-30T20:28:05Z","url":"https://github.com/python/mypy/issues/22334"}
| 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:91d5b797-8f03-f155-61c3-b748e9b12ff4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 90EC:59CDA:28684E2:34A45F8:6B000506 |
| html-safe-nonce | 7980cb0215c1f9c2125ce3fbc2148f7354c299bff0e09a90e9ee8fe28646dd73 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MEVDOjU5Q0RBOjI4Njg0RTI6MzRBNDVGODo2QjAwMDUwNiIsInZpc2l0b3JfaWQiOiI3NTMxNjcxNjYwMTMxMjU1NTU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9d2337eb9a131f26bc986f9f5be02064edf7804ed5eeff5be6840cc7c96beaea |
| hovercard-subject-tag | issue:6126462592 |
| 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/22334/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ddd51154a5711ce7a18226a08809b02a06823bec47b5795ccb732ec63d3c139/python/mypy/issues/22334 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ddd51154a5711ce7a18226a08809b02a06823bec47b5795ccb732ec63d3c139/python/mypy/issues/22334 |
| og:image:alt | from typing_extensions import sentinel Undefined = sentinel("Undefined") def none_if_undefined[_T](value: _T | Undefined) -> _T | None: return None if value is Undefined else value def func() -> No... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cdce8p |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width