Title: Problems with narrowing generic `T | Sequence[T]` · Issue #19202 · python/mypy · GitHub
Open Graph Title: Problems with narrowing generic `T | Sequence[T]` · Issue #19202 · python/mypy
X Title: Problems with narrowing generic `T | Sequence[T]` · Issue #19202 · python/mypy
Description: Trying to narrow T | Sequence[T] with isinstance clauses leads to type errors. I believe this has to do with the example below actually wanting T | (Sequence[T] & ¬T). So in the first clause, I guess mypy thinks this could be either L & ...
Open Graph Description: Trying to narrow T | Sequence[T] with isinstance clauses leads to type errors. I believe this has to do with the example below actually wanting T | (Sequence[T] & ¬T). So in the first clause, I gue...
X Description: Trying to narrow T | Sequence[T] with isinstance clauses leads to type errors. I believe this has to do with the example below actually wanting T | (Sequence[T] & ¬T). So in the first clause, I...
Opengraph URL: https://github.com/python/mypy/issues/19202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Problems with narrowing generic `T | Sequence[T]`","articleBody":"Trying to narrow `T | Sequence[T]` with `isinstance` clauses leads to type errors. I believe this has to do with the example below actually wanting `T | (Sequence[T] & ¬T)`.\n\nSo in the first clause, I guess `mypy` thinks this could be either `L & Foo = Foo` or `Sequence[L] & Foo`, which is not necessarily an instance of `L`. (so maybe not a bug but nevertheless a serious divergence between type checkers).\n\n**To Reproduce**\n\n```python\nfrom collections.abc import Sequence\n\nclass Foo: ...\n\ndef concat[L: Foo, R: Foo](\n left: L | Sequence[L],\n right: R | Sequence[R], /\n) -> list[L | R]:\n match left, right:\n case Foo(), Foo():\n return list( (left, right) ) # ❌\n case Foo(), [*rvalues]:\n return list( (left, *rvalues) ) # ❌\n case [*lvalues], Foo():\n return list( (*lvalues, right) ) # ❌\n case [*lvalues], [*rvalues]:\n return list( (*lvalues, *rvalues) )\n case _:\n raise TypeError\n \ndef concat2[L: Foo, R: Foo](\n left: L | Sequence[L],\n right: R | Sequence[R], /\n) -> list[L | R]:\n if isinstance(left, Foo) and isinstance(right, Foo):\n return list( (left, right) ) # ❌\n elif isinstance(left, Foo) and isinstance(right, Sequence):\n return list( (left, *right) ) # ❌\n elif isinstance(left, Sequence) and isinstance(right, Foo):\n return list( (*left, right) ) # ❌\n elif isinstance(left, Sequence) and isinstance(right, Sequence):\n return list( (*left, *right) )\n else:\n raise TypeError\n```\n\n\n**Expected Behavior**\n\nThis code passes without issues in [`pyright-playground`](https://pyright-play.net/?code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGUkBHAVySxiQChOZ4CccUAGJgwALih4p3ACZJgsbDAKoA2gBkJIsABooAJS2iAugApOUS1GTBUE9VAA%2B9Jq3ZINxnRash0AcwALOwMnFxY2DlV9LygAek4ASigAWgA%2Ba3QcNQdnGLEfSwgVGEDreVQ9PyC7QqtLZRwkYVFTRL1tNoL6nt8kVGYQLEzs0yhTG0qoauDk5KgAYihAGXI6%2BsbmzvaoVQAqEAA3AnhWHGNu3p6QfsHh%2BCzUMYmKvX2jk6QcOcsl1cvYfjNPbwd6nWJbC7-SzXAZDEaPca7EHHU5VAKzKDzX5rKwbHZI0GfWJ7Q4oomQqEw27wp4Esk4V6kj5fTE4hqAqAAfQp-xABCyzQAKgBPChIACiIHAIDZPU4cgUcHYKgATBojLoDBqzHVJvYwgwIu5PN56jMQvoDa5Ih4YnoEsl0vCNGF8nV0AosuhcKgCO5nrYOqJkn6ZGQcN7sn6OKZzUGwIkeX1YXcHk9Jmiat9Fis6kh7p6Iz7o0gA1NtCGsGGvcX-XHwm4OInZVS4fdRuMM1B9ujUNnsfV8x7w5Hff6u4bG0hK9Wi1G673483eTc22nEV3zf3c4OCyPazGJ9b3DP9-OY-XJzbl5dW6mO6YkS9u1vWbumknofymlARWLJdKnBAA) and [`ty-playground`](https://play.ty.dev/78ee9175-58d2-402a-94b2-3aa5f3fc35c0)\n\n**Actual Behavior**\n\n[`mypy-playground`](https://mypy-play.net/?mypy=master&python=3.12&gist=890e92eb66bfeb860943a3d9762f6821) emits 6 errors\n\n```\nmain.py:14: error: Argument 1 to \"SeqFoo\" has incompatible type \"tuple[Foo, Foo]\"; expected \"Iterable[L | R]\" [arg-type]\nmain.py:16: error: Argument 1 to \u003ctuple> has incompatible type \"Foo\"; expected \"L | R\" [arg-type]\nmain.py:18: error: Argument 2 to \u003ctuple> has incompatible type \"Foo\"; expected \"L | R\" [arg-type]\nmain.py:29: error: Argument 1 to \"SeqFoo\" has incompatible type \"tuple[Foo, Foo]\"; expected \"Iterable[L | R]\" [arg-type]\nmain.py:31: error: Argument 1 to \u003ctuple> has incompatible type \"Foo\"; expected \"L | R\" [arg-type]\nmain.py:33: error: Argument 2 to \u003ctuple> has incompatible type \"Foo\"; expected \"L | R\" [arg-type]\nFound 6 errors in 1 file (checked 1 source file)\n```\n\nAt the very least, the error messages are misleading, since the problematic bit are the arguments to `SeqFoo`, not to `tuple`.","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2025-06-02T09:37:15Z","url":"https://github.com/python/mypy/issues/19202"}
| 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:9b33cdb6-23ef-fed2-a2a3-9f37a989bbac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A3BA:14B836:C2CEBC:104478D:6B004E69 |
| html-safe-nonce | 14d4c218490d11fd5af34ac8f54c1ca5ad93397821fcf305a7cc218a7cc180b0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0JBOjE0QjgzNjpDMkNFQkM6MTA0NDc4RDo2QjAwNEU2OSIsInZpc2l0b3JfaWQiOiI2MTI2Njk4MTk0ODQ5MTczNTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | de6bb8e4be91837509afa5cefd5a0c3d1ed71e7be6e1a419de10ba9b3b52c461 |
| hovercard-subject-tag | issue:3109384527 |
| 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/19202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec5a2583a0f7d5328a0a0dd3e327f36d722d6ffca5bf7bbb0e583c68bafb7949/python/mypy/issues/19202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec5a2583a0f7d5328a0a0dd3e327f36d722d6ffca5bf7bbb0e583c68bafb7949/python/mypy/issues/19202 |
| og:image:alt | Trying to narrow T | Sequence[T] with isinstance clauses leads to type errors. I believe this has to do with the example below actually wanting T | (Sequence[T] & ¬T). So in the first clause, I gue... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width