Title: Spurious error with constrained type variables in generic functions · Issue #5493 · python/mypy · GitHub
Open Graph Title: Spurious error with constrained type variables in generic functions · Issue #5493 · python/mypy
X Title: Spurious error with constrained type variables in generic functions · Issue #5493 · python/mypy
Description: Reproduction: from typing import TypeVar, List class A: pass class B(A): pass class C: pass LooseType = TypeVar('LooseType', A, B, C) ListType = List[LooseType] def foo(xs: ListType[LooseType]) -> None: pass StrictType = TypeVar('StrictT...
Open Graph Description: Reproduction: from typing import TypeVar, List class A: pass class B(A): pass class C: pass LooseType = TypeVar('LooseType', A, B, C) ListType = List[LooseType] def foo(xs: ListType[LooseType]) -> ...
X Description: Reproduction: from typing import TypeVar, List class A: pass class B(A): pass class C: pass LooseType = TypeVar('LooseType', A, B, C) ListType = List[LooseType] def foo(xs: ListType[LooseTy...
Opengraph URL: https://github.com/python/mypy/issues/5493
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Spurious error with constrained type variables in generic functions","articleBody":"Reproduction:\r\n\r\n```python\r\nfrom typing import TypeVar, List\r\n\r\n\r\nclass A:\r\n pass\r\n\r\n\r\nclass B(A):\r\n pass\r\n\r\n\r\nclass C:\r\n pass\r\n\r\n\r\nLooseType = TypeVar('LooseType', A, B, C)\r\nListType = List[LooseType]\r\n\r\n\r\ndef foo(xs: ListType[LooseType]) -> None:\r\n pass\r\n\r\n\r\nStrictType = TypeVar('StrictType', B, C)\r\n\r\n\r\ndef bar(xs: ListType[StrictType]) -> None:\r\n foo(xs)\r\n```\r\n\r\nThis is denied with error reporting:\r\n\r\n```\r\ntest.py:28: error: Argument 1 to \"foo\" has incompatible type \"List[B]\"; expected \"List[A]\"\r\ntest.py:28: note: \"List\" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance\r\ntest.py:28: note: Consider using \"Sequence\" instead, which is covariant\r\n```\r\n\r\nIs this really not allowed? If `B` is not derived from `A`, then everything is ok. \r\n\r\nIn my original code, the container of `A`, `B` and `C` is `Dict[List[Tuple[LooseType, str]]]`. I cannot change `List` to `Sequence` because I need `append` method.\r\n\r\nIn addition, the notes disappear in complicated case.","author":{"url":"https://github.com/whoiscc","@type":"Person","name":"whoiscc"},"datePublished":"2018-08-17T12:57:07Z","url":"https://github.com/python/mypy/issues/5493"}
| 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:b0ef2e06-5771-8123-63b3-061f894f5ae7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EE14:30CC09:250FD75:3228772:6B002D84 |
| html-safe-nonce | e7a61ca97dc722bb8930c1fd5a1f44eb869ca0b4eed3d3cb3a06689e52db0501 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRTE0OjMwQ0MwOToyNTBGRDc1OjMyMjg3NzI6NkIwMDJEODQiLCJ2aXNpdG9yX2lkIjoiNjAwMDg3NDY1OTMyOTg3OTQyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 16b3e680dc56935b3b6120506fca655a03d007764fd04a2ee623c4ea7b778a7e |
| hovercard-subject-tag | issue:351583708 |
| 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/5493/issue_layout |
| twitter:image | https://opengraph.githubassets.com/642df4aeb05eacef0afb64306e796c632add84319956973130791bb9e5fdfe3d/python/mypy/issues/5493 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/642df4aeb05eacef0afb64306e796c632add84319956973130791bb9e5fdfe3d/python/mypy/issues/5493 |
| og:image:alt | Reproduction: from typing import TypeVar, List class A: pass class B(A): pass class C: pass LooseType = TypeVar('LooseType', A, B, C) ListType = List[LooseType] def foo(xs: ListType[LooseType]) -> ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | whoiscc |
| 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