René's URL Explorer Experiment


Title: TypeGuard on self does not narrow associated TypeVars · Issue #14425 · python/mypy · GitHub

Open Graph Title: TypeGuard on self does not narrow associated TypeVars · Issue #14425 · python/mypy

X Title: TypeGuard on self does not narrow associated TypeVars · Issue #14425 · python/mypy

Description: I have a class which is generic and has some runtime marker for determining the underlying type. I would like to use that marker to guide MyPy in validating the implementation of the class. So if I have a TypeGuard which confirms the cur...

Open Graph Description: I have a class which is generic and has some runtime marker for determining the underlying type. I would like to use that marker to guide MyPy in validating the implementation of the class. So if I...

X Description: I have a class which is generic and has some runtime marker for determining the underlying type. I would like to use that marker to guide MyPy in validating the implementation of the class. So if I...

Opengraph URL: https://github.com/python/mypy/issues/14425

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeGuard on self does not narrow associated TypeVars","articleBody":"I have a class which is generic and has some runtime marker for determining the underlying type. I would like to use that marker to guide MyPy in validating the implementation of the class. So if I have a TypeGuard which confirms the current object is generic in A (that is, TypeVar AB_T is bound to A), then it should be safe for the method which is annotated as returning an AB_T to return an A in that context.\r\n\r\nWhat I'm finding instead is that the TypeGuard correctly tells MyPy that the object which knows itself to be Gen[AB_T] is specifically a Gen[A], but it does not make the link that AB_T is A. As such, it refuses to return an A.\r\n\r\nI've tested the following example with MyPy 0.991 on Python 3.10\r\n\r\n```python\r\nfrom typing import Generic, Literal, overload, TypeVar\r\nfrom typing_extensions import TypeGuard\r\n\r\nclass A: ...\r\nclass B: ...\r\nAB_T = TypeVar(\"AB_T\", A, B)\r\n\r\nclass Gen(Generic[AB_T]):\r\n    # These two overloads ensure that the marker matches the generic type\r\n    @overload\r\n    def __init__(self: \"Gen[A]\", should_be_A: Literal[True]) -> None:\r\n        ...\r\n\r\n    @overload\r\n    def __init__(self: \"Gen[B]\", should_be_A: Literal[False]) -> None:\r\n        ...\r\n\r\n    def __init__(self: \"Gen[AB_T]\", should_be_A: bool) -> None:\r\n        self._should_be_A = should_be_A\r\n\r\n    def res(self) -> AB_T:\r\n        # This should return A() for a Gen[A] and B() for a Gen[B]\r\n        if Gen.gives_A(self): # This calling convention seems to be needed to narrow self\r\n            reveal_type(self) # and it now correctly understands that self is a Gen[A]\r\n            return A() # But this still complains about the scenario where self is a Gen[B]\r\n        elif Gen.gives_B(self):\r\n            reveal_type(self)\r\n            res: AB_T = B() # This also complains about the scenario where AB_T is A, even though we know it is B.\r\n            return res\r\n        else:\r\n            assert False\r\n\r\n    def gives_A(self) -> \"TypeGuard[Gen[A]]\":\r\n        return self._should_be_A\r\n\r\n    def gives_B(self) -> \"TypeGuard[Gen[B]]\":\r\n        return not self._should_be_A\r\n```\r\n\r\n**Actual Behavior**\r\n```\r\ngeneric_specialisation.py:26: note: Revealed type is \"generic_specialisation.Gen[generic_specialisation.A]\"\r\ngeneric_specialisation.py:27: error: Incompatible return value type (got \"A\", expected \"B\")  [return-value]\r\ngeneric_specialisation.py:29: note: Revealed type is \"generic_specialisation.Gen[generic_specialisation.B]\"\r\ngeneric_specialisation.py:30: error: Incompatible types in assignment (expression has type \"B\", variable has type \"A\")  [assignment]\r\n```","author":{"url":"https://github.com/JosiahKane","@type":"Person","name":"JosiahKane"},"datePublished":"2023-01-10T21:05:41Z","url":"https://github.com/python/mypy/issues/14425"}

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:2edf7f47-d273-f6fd-b59e-4369fe0742bc
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBE76:30DBED:31D49D0:41AF7D4:6B002A55
html-safe-nonceae3764dbae4b289f6be0f7af9eeb282b34d0d74ca56750550d0e91a028b0eb0f
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTc2OjMwREJFRDozMUQ0OUQwOjQxQUY3RDQ6NkIwMDJBNTUiLCJ2aXNpdG9yX2lkIjoiNTY3MzQ2Mzk1NTEyMTUxNTA5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacdaad84a19721b3e4a6b23d3135681f17350c4480c5794f1329638fa8ae84007d
hovercard-subject-tagissue:1528008172
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/14425/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8103112762ae88c78defd4aa792a0ff9fd52e48c617bb06e5a85c0255b9c46b2/python/mypy/issues/14425
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8103112762ae88c78defd4aa792a0ff9fd52e48c617bb06e5a85c0255b9c46b2/python/mypy/issues/14425
og:image:altI have a class which is generic and has some runtime marker for determining the underlying type. I would like to use that marker to guide MyPy in validating the implementation of the class. So if I...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameJosiahKane
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
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
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/14425#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F14425
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%2F14425
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/14425
Reloadhttps://github.com/python/mypy/issues/14425
Reloadhttps://github.com/python/mypy/issues/14425
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 requests547 (547)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/14425
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/14425
TypeGuard on self does not narrow associated TypeVarshttps://github.com/python/mypy/issues/14425#top
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/JosiahKane
JosiahKanehttps://github.com/JosiahKane
on Jan 10, 2023https://github.com/python/mypy/issues/14425#issue-1528008172
JosiahKanehttps://github.com/JosiahKane
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
on Jan 10, 2023https://github.com/python/mypy/issues/14425#event-8195900774
https://github.com/JosiahKane
JosiahKanehttps://github.com/JosiahKane
on Jan 10, 2023https://github.com/python/mypy/issues/14425#issuecomment-1377886366
https://github.com/A5rocks
A5rockshttps://github.com/A5rocks
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1379831235
https://github.com/JosiahKane
JosiahKanehttps://github.com/JosiahKane
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1380000260
@A5rockshttps://github.com/A5rocks
this pointhttps://peps.python.org/pep-0647/#narrowing-of-implicit-self-and-cls-parameters
https://github.com/A5rocks
A5rockshttps://github.com/A5rocks
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1380469826
#1539https://github.com/python/mypy/issues/1539
#11230https://github.com/python/mypy/issues/11230
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1380921170
@JosiahKanehttps://github.com/JosiahKane
enhancement request in pyrighthttps://github.com/microsoft/pyright/issues/4448
@A5rockshttps://github.com/A5rocks
https://github.com/A5rocks
A5rockshttps://github.com/A5rocks
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1381023160
https://github.com/JosiahKane
JosiahKanehttps://github.com/JosiahKane
on Jan 12, 2023https://github.com/python/mypy/issues/14425#issuecomment-1381070893
@erictrauthttps://github.com/erictraut
hauntsaninjahttps://github.com/hauntsaninja
on Jan 31, 2023https://github.com/python/mypy/issues/14425#event-8396178232
More helpful type guards (#14238)https://github.com/python/mypy/commit/28c67cbeb09a14865ccc17cc1d13debcdf744940
#14238https://github.com/python/mypy/pull/14238
28c67cbhttps://github.com/python/mypy/commit/28c67cbeb09a14865ccc17cc1d13debcdf744940
https://github.com/ezyang
ezyanghttps://github.com/ezyang
on Jan 29, 2024https://github.com/python/mypy/issues/14425#issuecomment-1914852707
https://github.com/hauntsaninja
hauntsaninjahttps://github.com/hauntsaninja
on Jan 30, 2024https://github.com/python/mypy/issues/14425#issuecomment-1915804158
@ezyanghttps://github.com/ezyang
https://github.com/ezyang
ezyanghttps://github.com/ezyang
on Jan 30, 2024https://github.com/python/mypy/issues/14425#issuecomment-1915921333
https://github.com/hauntsaninja
hauntsaninjahttps://github.com/hauntsaninja
on Jan 30, 2024https://github.com/python/mypy/issues/14425#issuecomment-1916102754
https://github.com/ezyang
ezyanghttps://github.com/ezyang
on Jan 31, 2024https://github.com/python/mypy/issues/14425#issuecomment-1919460967
ezyanghttps://github.com/ezyang
on Jan 31, 2024https://github.com/python/mypy/issues/14425#event-11659040747
Update base for Update on "Some minor type stub improvements"https://github.com/pytorch/pytorch/commit/b2176093f745bf795531beaa849c8421f7ddb889
b217609https://github.com/pytorch/pytorch/commit/b2176093f745bf795531beaa849c8421f7ddb889
Update on "Some minor type stub improvements"https://github.com/pytorch/pytorch/commit/5e03519f055523e40fce301f05261ac57768a291
5e03519https://github.com/pytorch/pytorch/commit/5e03519f055523e40fce301f05261ac57768a291
pytorchmergebothttps://github.com/pytorchmergebot
on Jan 31, 2024https://github.com/python/mypy/issues/14425#event-11661249563
Some minor type stub improvements (#118529)https://github.com/pytorch/pytorch/commit/c978f38bd4aedeff4ee9ae693349217daea01412
#118529https://github.com/pytorch/pytorch/pull/118529
c978f38https://github.com/pytorch/pytorch/commit/c978f38bd4aedeff4ee9ae693349217daea01412
facebook-github-bothttps://github.com/facebook-github-bot
on Feb 1, 2024https://github.com/python/mypy/issues/14425#event-11673508860
Some minor type stub improvements (#118529)https://github.com/pytorch/benchmark/commit/90cedbeb4256da7e69656931567f0fb84adbbede
90cedbehttps://github.com/pytorch/benchmark/commit/90cedbeb4256da7e69656931567f0fb84adbbede
ezyanghttps://github.com/ezyang
on Feb 3, 2024https://github.com/python/mypy/issues/14425#event-11693489277
Update base for Update on "Some minor type stub improvements"https://github.com/pytorch/pytorch/commit/ebdfc275016698c556322a39903497f139da3f18
ebdfc27https://github.com/pytorch/pytorch/commit/ebdfc275016698c556322a39903497f139da3f18
Update on "Some minor type stub improvements"https://github.com/pytorch/pytorch/commit/f7e3128d320bde490d892c7024536973b0c101e4
f7e3128https://github.com/pytorch/pytorch/commit/f7e3128d320bde490d892c7024536973b0c101e4
pytorchmergebothttps://github.com/pytorchmergebot
on Feb 4, 2024https://github.com/python/mypy/issues/14425#event-11693755478
Some minor type stub improvements (#118529)https://github.com/pytorch/pytorch/commit/abc09b27b953427a5845069c70be5c646923016a
#118529https://github.com/pytorch/pytorch/pull/118529
abc09b2https://github.com/pytorch/pytorch/commit/abc09b27b953427a5845069c70be5c646923016a
facebook-github-bothttps://github.com/facebook-github-bot
on Feb 6, 2024https://github.com/python/mypy/issues/14425#event-11718065846
Some minor type stub improvements (#118529)https://github.com/pytorch/benchmark/commit/b30d690bb98cdd717288ba783a1d6f97654dfd29
b30d690https://github.com/pytorch/benchmark/commit/b30d690bb98cdd717288ba783a1d6f97654dfd29
pytorch-bothttps://github.com/apps/pytorch-bot
on Feb 8, 2024https://github.com/python/mypy/issues/14425#event-11751996661
Some minor type stub improvements (#118529)https://github.com/pytorch/pytorch/commit/70724a0bfc26b49e0bf5cf06947c33fcc8da46d4
#118529https://github.com/pytorch/pytorch/pull/118529
70724a0https://github.com/pytorch/pytorch/commit/70724a0bfc26b49e0bf5cf06947c33fcc8da46d4
Some minor type stub improvements (#118529)https://github.com/pytorch/pytorch/commit/fd3518b2b8f634184e0ec636ba84d7c92d9c4e23
#118529https://github.com/pytorch/pytorch/pull/118529
fd3518bhttps://github.com/pytorch/pytorch/commit/fd3518b2b8f634184e0ec636ba84d7c92d9c4e23
https://github.com/ReSqAr
ReSqArhttps://github.com/ReSqAr
on May 27, 2024https://github.com/python/mypy/issues/14425#issuecomment-2133341262
https://github.com/eternal-sorrow
eternal-sorrowhttps://github.com/eternal-sorrow
on May 1, 2026https://github.com/python/mypy/issues/14425#issuecomment-4358546656
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/14425
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/14425
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.