Title: Indexing TypeVars / need a workaround for higher-kindedness · Issue #6066 · python/mypy · GitHub
Open Graph Title: Indexing TypeVars / need a workaround for higher-kindedness · Issue #6066 · python/mypy
X Title: Indexing TypeVars / need a workaround for higher-kindedness · Issue #6066 · python/mypy
Description: I'm using mypy 0.630. I have a container, which can contain various types, some of which contain further types. I want the root of the container to be able to return grandchild types, or failing that to be able to return fully-specified ...
Open Graph Description: I'm using mypy 0.630. I have a container, which can contain various types, some of which contain further types. I want the root of the container to be able to return grandchild types, or failing th...
X Description: I'm using mypy 0.630. I have a container, which can contain various types, some of which contain further types. I want the root of the container to be able to return grandchild types, or failin...
Opengraph URL: https://github.com/python/mypy/issues/6066
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Indexing TypeVars / need a workaround for higher-kindedness","articleBody":"I'm using mypy 0.630. \r\n\r\nI have a container, which can contain various types, some of which contain further types. I want the root of the container to be able to return grandchild types, or failing that to be able to return fully-specified child types. My first thought doesn't work:\r\n\r\n```python\r\nfrom typing import TypeVar, Generic, List\r\n\r\nContainedType = TypeVar('ContainedType', bound='ContainedCls')\r\nSubContained = TypeVar('SubContained')\r\nT = TypeVar('T')\r\n\r\nclass Root_wrong(Generic[ContainedType[SubContained]]):\r\n def __init__(self, ct):\r\n # type: (ContainedType[SubContained]) -> None\r\n self.contained = ct\r\n\r\n def iter_subcontained(self):\r\n # type: () -> List[SubContained]\r\n return self.contained.items\r\n\r\nclass ContainedCls(Generic[T]):\r\n def __init__(self, items):\r\n # type: (List[T]) -> None\r\n self.items = items\r\n```\r\n\r\nBecause `TypeVar`s can't be indexed.\r\n\r\nThis also doesn't work, because `iter_subcontained` can't be typed:\r\n\r\n```python\r\nclass Root(Generic[ContainedType]):\r\n def __init__(self, ct):\r\n # type: (ContainedType) -> None\r\n # I know because of the bound that ContainedType will have a parameter of its own, but I\r\n # can't refer to it in the `class Root` declaration because TypeVars can't be indexed.\r\n # and the index to Generic has to be a typevar\r\n self.contained = ct\r\n\r\n def iter_subcontained(self):\r\n # what goes here?\r\n return self.contained.items\r\n\r\n\r\nreveal_type(Root(ContainedCls([1])).iter_subcontained()) # Any\r\n```\r\n\r\nAlas, this also doesn't work:\r\n\r\n```python\r\nclass Root2(Generic[ContainedType, SubContained]):\r\n def __init__(self, ct):\r\n # type: (ContainedType) -> None\r\n # I know because of the bound that ContainedType will have a parameter of its own, but I\r\n # can't refer to it in the `class Root` declaration because TypeVars can't be indexed.\r\n # and the index to Generic has to be a typevar\r\n self.contained = ct\r\n\r\n def iter_subcontained(self):\r\n # type: () -> List[SubContained]\r\n return self.contained.items\r\n\r\n\r\nreveal_type(Root2(ContainedCls([1])).iter_subcontained()) # builtins.list[\u003cnothing>]\r\n\r\n# not matching is not an error\r\nreveal_type(Root2[ContainedType[int], str](ContainedCls([1])).iter_subcontained()) # builtins.list[str]\r\n```\r\n\r\nThis almost works:\r\n\r\n```python\r\nclass Root3(Generic[T]):\r\n def __init__(self, ct1, ct2):\r\n # type: (ContainedCls[T], ContainedCls[T]) -> None\r\n self.contained = ct1\r\n self.contained2 = ct2\r\n\r\n def iter_subcontained(self):\r\n # type: () -> List[T]\r\n return self.contained.items\r\n\r\n\r\nclass ContainedClsPrime(ContainedCls):\r\n pass\r\n\r\n\r\n# no way to ensure that ct1 and ct2 are the same container type\r\n# in fact, they aren't even constrained to have the same T!\r\nx = Root3[int](ContainedCls([1]), ContainedClsPrime(['definitely not an int']))\r\nreveal_type(x.iter_subcontained()) # builtins.list[str]\r\nreveal_type(x.contained2.items[0]) # builtins.int* !!!\r\n```\r\n\r\nBut it doesn't constrain the two `ContainedCls[T]`s to be the *same* `ContainedCls` subtype, and it doesn't (this really surprised me) even constrain them to be containing the same `T`! It also seems wrong to be able to make `Root3` generic *only* in the leaf types, and not in any of the intermediate types.","author":{"url":"https://github.com/bwo","@type":"Person","name":"bwo"},"datePublished":"2018-12-13T20:03:15Z","url":"https://github.com/python/mypy/issues/6066"}
| 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:17c5be24-98ee-a8fb-1d7f-d000cf6a1fb2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3AC:2AC6FB:10AA8E9:160346D:6B000CA3 |
| html-safe-nonce | 83ef48826e067bd1732eda0976d95cc5491469e78726b5172aabaf5b7590a310 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0FDOjJBQzZGQjoxMEFBOEU5OjE2MDM0NkQ6NkIwMDBDQTMiLCJ2aXNpdG9yX2lkIjoiNTE2NjI0NjA0Mjc5MzkzODA4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc41fb95f748f4a5fe9951f5c9ad9ee6761e750f9cc40668c8779d018b9be7c4 |
| hovercard-subject-tag | issue:390837740 |
| 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/6066/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d8af6c3a378884d1008cd432697cdb98c60d784627e20d459ef2447520ccfeb8/python/mypy/issues/6066 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d8af6c3a378884d1008cd432697cdb98c60d784627e20d459ef2447520ccfeb8/python/mypy/issues/6066 |
| og:image:alt | I'm using mypy 0.630. I have a container, which can contain various types, some of which contain further types. I want the root of the container to be able to return grandchild types, or failing th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bwo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width