Title: Error If Return Annotation Is Provided (Generics + TypeVar + isinstance) · Issue #12873 · python/mypy · GitHub
Open Graph Title: Error If Return Annotation Is Provided (Generics + TypeVar + isinstance) · Issue #12873 · python/mypy
X Title: Error If Return Annotation Is Provided (Generics + TypeVar + isinstance) · Issue #12873 · python/mypy
Description: I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the "isinstance" block. Mypy gives an error on the last line of ErrorIfReturnIsAnnotated.foo() while it doesn't give any errors fo...
Open Graph Description: I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the "isinstance" block. Mypy gives an error on the last line of ErrorIfReturnIsAnnotated.f...
X Description: I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the "isinstance" block. Mypy gives an error on the last line of ErrorIfReturnIsA...
Opengraph URL: https://github.com/python/mypy/issues/12873
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error If Return Annotation Is Provided (Generics + TypeVar + isinstance)","articleBody":"I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the \"isinstance\" block.\r\nMypy gives an error on the last line of ErrorIfReturnIsAnnotated.foo() while it doesn't give any errors for WorksIfNoReturnAnnotation.foo()\r\n\r\nThe only difference in those two classes are that one of them declares an explicit None return annotation.\r\n\r\nEnvironment\r\n- Mypy version used: 0.950 and 0.960\r\n- Mypy command-line flags: None\r\n- Mypy configuration options from `mypy.ini` (and other config files): None\r\n- Python version used: Python 3.10.4\r\n- Operating system and version: Windows 10 (Version 21H2)\r\n\r\nFull simplified test code to reproduce the bug\r\n\r\n```\r\nfrom abc import ABCMeta\r\nfrom typing import Generic, TypeVar, Union\r\n\r\n\r\nclass BaseEngine(metaclass=ABCMeta):\r\n pass\r\n\r\n\r\nclass BaseEngineVariantA(BaseEngine):\r\n pass\r\n\r\n\r\nclass BaseEngineVariantB(BaseEngine):\r\n pass\r\n\r\n\r\nclass VariantAConcreteEngine(BaseEngineVariantA):\r\n pass\r\n\r\n\r\nT = TypeVar('T', bound=Union[BaseEngineVariantA, BaseEngineVariantB])\r\n\r\n\r\nclass IBase(Generic[T], metaclass=ABCMeta):\r\n def __init__(self, engine: T):\r\n self.engine = engine\r\n\r\n\r\nclass IAction(IBase[T], metaclass=ABCMeta):\r\n pass\r\n\r\n\r\nclass ErrorIfReturnIsAnnotated(IBase[T]):\r\n def __init__(self, something: T, actions_injected: type[IAction[T]]) -> None:\r\n self.something = something\r\n self.actions_injected = actions_injected\r\n\r\n def foo(self) -> None:\r\n if not isinstance(self.something, VariantAConcreteEngine):\r\n raise Exception(\"HI\")\r\n\r\n self.actions_injected(self.something) # Gives a mypy error of - error: Argument 1 to \"IAction\" has incompatible type \"VariantAConcreteEngine\"; expected \"T\"\r\n\r\n\r\nclass WorksIfNoReturnAnnotation(IBase[T]):\r\n def __init__(self, something: T, actions_injected: type[IAction[T]]) -> None:\r\n self.something = something\r\n self.actions_injected = actions_injected\r\n\r\n def foo(self):\r\n if not isinstance(self.something, VariantAConcreteEngine):\r\n raise Exception(\"HI\")\r\n\r\n self.actions_injected(self.something) # No errors\r\n\r\n```","author":{"url":"https://github.com/julzt0244","@type":"Person","name":"julzt0244"},"datePublished":"2022-05-26T13:59:05Z","url":"https://github.com/python/mypy/issues/12873"}
| 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:22527953-0327-7252-ba91-ef3bd60f314c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D528:308C72:6B86F:94E51:6B005136 |
| html-safe-nonce | 61457279203bea2586204d09dca8d79a46f81d647c681d31b4ca4e89acf5e980 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTI4OjMwOEM3Mjo2Qjg2Rjo5NEU1MTo2QjAwNTEzNiIsInZpc2l0b3JfaWQiOiI1NTA3NjQ5NzM3NTY3Nzg1MjcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4cac91af69af9975b656f452a7cd172c182f6eabb38225454734e9e03d6be2a5 |
| hovercard-subject-tag | issue:1249610591 |
| 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/12873/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b0dc22b0838060b288c1f1916ab1cbcaccfd4df42aab2152820acb9d5f80c58/python/mypy/issues/12873 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b0dc22b0838060b288c1f1916ab1cbcaccfd4df42aab2152820acb9d5f80c58/python/mypy/issues/12873 |
| og:image:alt | I get this strange error whereby adding a return annotation causes mypy to fail to identify the code after the "isinstance" block. Mypy gives an error on the last line of ErrorIfReturnIsAnnotated.f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | julzt0244 |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width