Title: TypeVar error · Issue #12882 · python/mypy · GitHub
Open Graph Title: TypeVar error · Issue #12882 · python/mypy
X Title: TypeVar error · Issue #12882 · python/mypy
Description: Following up on my previous issue, it seems TypeVars are bugged and do not allow for more specific subclasses of one of the bound types specific, unlike using Unions alone? See the 2 generic types represented by the variables "CombinedTy...
Open Graph Description: Following up on my previous issue, it seems TypeVars are bugged and do not allow for more specific subclasses of one of the bound types specific, unlike using Unions alone? See the 2 generic types ...
X Description: Following up on my previous issue, it seems TypeVars are bugged and do not allow for more specific subclasses of one of the bound types specific, unlike using Unions alone? See the 2 generic types ...
Opengraph URL: https://github.com/python/mypy/issues/12882
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeVar error","articleBody":"Following up on my previous [issue](https://github.com/python/mypy/issues/12873), it seems TypeVars are bugged and do not allow for more specific subclasses of one of the bound types specific, unlike using Unions alone?\r\n\r\nSee the 2 generic types represented by the variables \"CombinedTypes\" and \"T\" \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\nCombinedTypes = Union[BaseEngineVariantA, BaseEngineVariantB]\r\nT = TypeVar('T', bound=CombinedTypes)\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 ErrorIfUsingTypeVar(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 WorksFineUsingUnionsAlone(IBase[CombinedTypes]):\r\n def __init__(self, something: CombinedTypes, actions_injected: type[IAction[CombinedTypes]]) -> 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) # No error if using unions alone\r\n\r\n```","author":{"url":"https://github.com/julzt0244","@type":"Person","name":"julzt0244"},"datePublished":"2022-05-27T12:42:03Z","url":"https://github.com/python/mypy/issues/12882"}
| 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:2abf73cd-d015-0ee6-9b04-c29cfecad194 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A080:26A2A:59C74:7D52A:6B004E51 |
| html-safe-nonce | 0039176096a7a7c624ca52a5f0cf62a524e9f600c5e3457500b0938158a15dff |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDgwOjI2QTJBOjU5Qzc0OjdENTJBOjZCMDA0RTUxIiwidmlzaXRvcl9pZCI6IjQ0ODQ4MDM0NjA3MDg5NzgyNTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ca0392cd03926a00f56a466628948817461961bb54ddc54d978f418cc7d442a8 |
| hovercard-subject-tag | issue:1250724674 |
| 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/12882/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c7b998f617d3a2f7ba0288e43e09cd4404923861bbaf8db5dd4d323430d2aed9/python/mypy/issues/12882 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c7b998f617d3a2f7ba0288e43e09cd4404923861bbaf8db5dd4d323430d2aed9/python/mypy/issues/12882 |
| og:image:alt | Following up on my previous issue, it seems TypeVars are bugged and do not allow for more specific subclasses of one of the bound types specific, unlike using Unions alone? See the 2 generic types ... |
| 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 | 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