Title: Type-narrowing in a union sometimes fails · Issue #19265 · python/mypy · GitHub
Open Graph Title: Type-narrowing in a union sometimes fails · Issue #19265 · python/mypy
X Title: Type-narrowing in a union sometimes fails · Issue #19265 · python/mypy
Description: Possibly related: #10680 Bug Report, To Reproduce, & Actual Behaviour The following snippet doesn't type-narrow properly (see mypy Playground): from typing import Protocol type StaticCallback[**P, R] = Callback[P, R] | staticmethod[P, R]...
Open Graph Description: Possibly related: #10680 Bug Report, To Reproduce, & Actual Behaviour The following snippet doesn't type-narrow properly (see mypy Playground): from typing import Protocol type StaticCallback[**P, ...
X Description: Possibly related: #10680 Bug Report, To Reproduce, & Actual Behaviour The following snippet doesn't type-narrow properly (see mypy Playground): from typing import Protocol type StaticCallba...
Opengraph URL: https://github.com/python/mypy/issues/19265
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type-narrowing in a union sometimes fails","articleBody":"Possibly related: #10680 \n\n**Bug Report, To Reproduce, & Actual Behaviour**\n\nThe following snippet doesn't type-narrow properly (see [mypy Playground](https://mypy-play.net/?mypy=latest&python=3.12&flags=strict-bytes&gist=cf1f8558066ff81d9675987b137e0ba7)):\n\n```python\nfrom typing import Protocol\n\ntype StaticCallback[**P, R] = Callback[P, R] | staticmethod[P, R]\n\nclass Callback[**P, R](Protocol):\n def __call__(self, /, *args: P.args, **kwargs: P.kwargs) -> R: ...\n\ndef try_return_type(val: StaticCallback[[str], str], /) -> type[str]:\n if isinstance(val, type) and issubclass(val, str):\n return val # E: Incompatible return value type (got \"\u003csubclass of \"__main__.Callback[[builtins.str], builtins.str]\" and \"builtins.type\"> | \u003csubclass of \"builtins.staticmethod[[builtins.str], builtins.str]\" and \"builtins.type\">\", expected \"type[str]\") [return-value]\n assert False\n```\n\nThis has something to do with the second member of the `StaticCallback` union. I've tried with these other random examples of `\u003cT>` in `type StaticCallback[**P, R] = Callback[P, R] | \u003cT>`, but the reason for failure still isn't obvious to me:\n\nNarrowing succeeds:\n* `\u003cT> = int`\n* `\u003cT> = None`\n* `\u003cT> = dict[str, object]`\n* `\u003cT> = dict[R, R]`\n\nNarrowing fails:\n* `\u003cT> = staticmethod[P, R]` (same as the snippet)\n* `\u003cT> = bytes` (fails regardless of whether `--strict-bytes` is on)\n* `\u003cT> = list[object]`\n* `\u003cT> = list[R]`\n\n**Expected Behavior**\n\nNo errors\n\n**Your Environment**\n\n- Mypy version used: 1.13—1.16\n- Mypy command-line flags: None\n- Mypy configuration options from `mypy.ini` (and other config files): None\n- Python version used: 3.9, 3.12\n","author":{"url":"https://github.com/bzoracler","@type":"Person","name":"bzoracler"},"datePublished":"2025-06-10T05:54:50Z","url":"https://github.com/python/mypy/issues/19265"}
| 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:4b1ad1cb-1552-3151-1022-fa109dce2c9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C5A0:120A77:218781F:2DA4646:6B0025AF |
| html-safe-nonce | f2490af9f4fe2d04a6bb2e0195a1645069fd96964c895c97a91276177596a2da |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNUEwOjEyMEE3NzoyMTg3ODFGOjJEQTQ2NDY6NkIwMDI1QUYiLCJ2aXNpdG9yX2lkIjoiMTgxNDc1MTExMTczOTg3Njc4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5a25fb2905ade16ea82abe22d7e862475501f3a3fa1acb061b64587e583cb427 |
| hovercard-subject-tag | issue:3132237754 |
| 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/19265/issue_layout |
| twitter:image | https://opengraph.githubassets.com/01f5859176155db5fa877e89ed43a4fb97dc98c188c32d262af7e94897f19f49/python/mypy/issues/19265 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/01f5859176155db5fa877e89ed43a4fb97dc98c188c32d262af7e94897f19f49/python/mypy/issues/19265 |
| og:image:alt | Possibly related: #10680 Bug Report, To Reproduce, & Actual Behaviour The following snippet doesn't type-narrow properly (see mypy Playground): from typing import Protocol type StaticCallback[**P, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bzoracler |
| 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