René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9b33cdb6-23ef-fed2-a2a3-9f37a989bbac
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA3BA:14B836:C2CEBC:104478D:6B004E69
html-safe-nonce14d4c218490d11fd5af34ac8f54c1ca5ad93397821fcf305a7cc218a7cc180b0
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0JBOjE0QjgzNjpDMkNFQkM6MTA0NDc4RDo2QjAwNEU2OSIsInZpc2l0b3JfaWQiOiI2MTI2Njk4MTk0ODQ5MTczNTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacde6bb8e4be91837509afa5cefd5a0c3d1ed71e7be6e1a419de10ba9b3b52c461
hovercard-subject-tagissue:3109384527
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/19202/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ec5a2583a0f7d5328a0a0dd3e327f36d722d6ffca5bf7bbb0e583c68bafb7949/python/mypy/issues/19202
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ec5a2583a0f7d5328a0a0dd3e327f36d722d6ffca5bf7bbb0e583c68bafb7949/python/mypy/issues/19202
og:image:altTrying 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerandolf-scholz
hostnamegithub.com
expected-hostnamegithub.com
Nonef9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseb27c5dde30676028abd5c5fc53ce5c9b625f8281
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/19202#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F19202
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F19202
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/19202
Reloadhttps://github.com/python/mypy/issues/19202
Reloadhttps://github.com/python/mypy/issues/19202
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests546 (546)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/19202
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/19202
Problems with narrowing generic T | Sequence[T]https://github.com/python/mypy/issues/19202#top
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/randolf-scholz
randolf-scholzhttps://github.com/randolf-scholz
on Jun 2, 2025https://github.com/python/mypy/issues/19202#issue-3109384527
pyright-playgroundhttps://pyright-play.net/?code=GYJw9gtgBAxmA28CmMAuBLMA7AzgOgEMAjGKdCABzBFSgGUkBHAVySxiQChOZ4CccUAGJgwALih4p3ACZJgsbDAKoA2gBkJIsABooAJS2iAugApOUS1GTBUE9VAA%2B9Jq3ZINxnRash0AcwALOwMnFxY2DlV9LygAek4ASigAWgA%2Ba3QcNQdnGLEfSwgVGEDreVQ9PyC7QqtLZRwkYVFTRL1tNoL6nt8kVGYQLEzs0yhTG0qoauDk5KgAYihAGXI6%2BsbmzvaoVQAqEAA3AnhWHGNu3p6QfsHh%2BCzUMYmKvX2jk6QcOcsl1cvYfjNPbwd6nWJbC7-SzXAZDEaPca7EHHU5VAKzKDzX5rKwbHZI0GfWJ7Q4oomQqEw27wp4Esk4V6kj5fTE4hqAqAAfQp-xABCyzQAKgBPChIACiIHAIDZPU4cgUcHYKgATBojLoDBqzHVJvYwgwIu5PN56jMQvoDa5Ih4YnoEsl0vCNGF8nV0AosuhcKgCO5nrYOqJkn6ZGQcN7sn6OKZzUGwIkeX1YXcHk9Jmiat9Fis6kh7p6Iz7o0gA1NtCGsGGvcX-XHwm4OInZVS4fdRuMM1B9ujUNnsfV8x7w5Hff6u4bG0hK9Wi1G673483eTc22nEV3zf3c4OCyPazGJ9b3DP9-OY-XJzbl5dW6mO6YkS9u1vWbumknofymlARWLJdKnBAA
ty-playgroundhttps://play.ty.dev/78ee9175-58d2-402a-94b2-3aa5f3fc35c0
mypy-playgroundhttps://mypy-play.net/?mypy=master&python=3.12&gist=890e92eb66bfeb860943a3d9762f6821
randolf-scholzhttps://github.com/randolf-scholz
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
on Jun 2, 2025https://github.com/python/mypy/issues/19202#event-17928583563
https://github.com/sterliakov
sterliakovhttps://github.com/sterliakov
on Jun 3, 2025https://github.com/python/mypy/issues/19202#issuecomment-2933000262
#15151https://github.com/python/mypy/issues/15151
#19183https://github.com/python/mypy/pull/19183
sterliakovhttps://github.com/sterliakov
duplicatehttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Aduplicate
Type narrowing of 'TypeVar' causes '[return-value]' error #15151https://github.com/python/mypy/issues/15151
on Jun 3, 2025https://github.com/python/mypy/issues/19202#event-17945639016
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/19202
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/19202
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.