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
Domain: github.com
{"@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-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:2edf7f47-d273-f6fd-b59e-4369fe0742bc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE76:30DBED:31D49D0:41AF7D4:6B002A55 |
| html-safe-nonce | ae3764dbae4b289f6be0f7af9eeb282b34d0d74ca56750550d0e91a028b0eb0f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTc2OjMwREJFRDozMUQ0OUQwOjQxQUY3RDQ6NkIwMDJBNTUiLCJ2aXNpdG9yX2lkIjoiNTY3MzQ2Mzk1NTEyMTUxNTA5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | daad84a19721b3e4a6b23d3135681f17350c4480c5794f1329638fa8ae84007d |
| hovercard-subject-tag | issue:1528008172 |
| 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/14425/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8103112762ae88c78defd4aa792a0ff9fd52e48c617bb06e5a85c0255b9c46b2/python/mypy/issues/14425 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8103112762ae88c78defd4aa792a0ff9fd52e48c617bb06e5a85c0255b9c46b2/python/mypy/issues/14425 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JosiahKane |
| 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