Title: mypy too strict in enforcing generic return types when they are unions · Issue #19239 · python/mypy · GitHub
Open Graph Title: mypy too strict in enforcing generic return types when they are unions · Issue #19239 · python/mypy
X Title: mypy too strict in enforcing generic return types when they are unions · Issue #19239 · python/mypy
Description: Bug Report mypy is overly strict in enforcing union return types from generic functions. If a function both takes and returns a value of type T, which is a union of two types, and does any guarding to check which of the possible types th...
Open Graph Description: Bug Report mypy is overly strict in enforcing union return types from generic functions. If a function both takes and returns a value of type T, which is a union of two types, and does any guarding...
X Description: Bug Report mypy is overly strict in enforcing union return types from generic functions. If a function both takes and returns a value of type T, which is a union of two types, and does any guarding...
Opengraph URL: https://github.com/python/mypy/issues/19239
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy too strict in enforcing generic return types when they are unions","articleBody":"\u003c!--\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\nPlease consider:\n\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\n- asking on gitter chat: https://gitter.im/python/typing\n-->\n\n**Bug Report**\n\n\u003c!--\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\n\nIf the project you encountered the issue in is open source, please provide a link to the project.\n-->\n\nmypy is overly strict in enforcing union return types from generic functions.\n\nIf a function both takes and returns a value of type T, which is a union of two types, and does any guarding to check which of the possible types the value is before returning it, mypy emits an \"Incompatible return type\" error.\n\n**To Reproduce**\n\nThis happens with checks using `isinstance` or with `is None` e.g.:\n\n```python\ndef generic_function[T: str | int](value: T) -> T:\n if isinstance(value, int):\n return value\n return value\n```\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=470d29ce79af159a1950e5f47aac2798\n\nand\n\n```python\ndef generic_function[T: str | None](value: T) -> T:\n if value is None:\n return value\n return value\n```\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=16f08fc7cc619fa2797d12d6c50e2103\n\n\n**Expected Behavior**\n\n\u003c!--\nHow did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.\nWrite down what you thought would happen. If you expected no errors, delete this section.\n-->\n\nmypy should not emit an error, because the input and output types are still correct, the guard just leads to an early return.\n\nI suppose more specifically I would expect that guards on the input type for a generic which takes and returns the same type should also be guards on the output type.\n\n**Actual Behavior**\n\n\u003c!-- What went wrong? Paste mypy's output. -->\n\n```error: Incompatible return value type (got \"int\", expected \"T\") [return-value]```\nand\n```error: Incompatible return value type (got \"None\", expected \"T\") [return-value]```\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.16.0\n- Mypy command-line flags:\n- Mypy configuration options from `mypy.ini` (and other config files):\n```toml\n[tool.mypy]\nstrict = true\npython_version = \"3.12\"\nplugins = ['pydantic.mypy']\n```\n- Python version used: Python 3.12\n\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/thesketh","@type":"Person","name":"thesketh"},"datePublished":"2025-06-05T14:18:06Z","url":"https://github.com/python/mypy/issues/19239"}
| 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:9df42eb7-2431-780b-be9d-96a44220ce36 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C0A:2A0045:3620A22:47D7194:6B004721 |
| html-safe-nonce | 5238adb837baeea780731ccf673daf00273de5355ff48c6feb435c839f1e01b5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzBBOjJBMDA0NTozNjIwQTIyOjQ3RDcxOTQ6NkIwMDQ3MjEiLCJ2aXNpdG9yX2lkIjoiMjg1NjQxODc4NTY5MzI4ODIyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a2ce4ece4c55937fb861f1500e7f166c5388f2062705e4bc648c3ec7f5cccca2 |
| hovercard-subject-tag | issue:3121445559 |
| 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/19239/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d2f32280f627570ab34af3960674a59d7ee1672384ed300001bc3f50c33b537d/python/mypy/issues/19239 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d2f32280f627570ab34af3960674a59d7ee1672384ed300001bc3f50c33b537d/python/mypy/issues/19239 |
| og:image:alt | Bug Report mypy is overly strict in enforcing union return types from generic functions. If a function both takes and returns a value of type T, which is a union of two types, and does any guarding... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thesketh |
| 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