Indexing TypeVars / need a workaround for higher-kindedness
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
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 child types. My first thought doesn't work:
from typing import TypeVar, Generic, List
ContainedType = TypeVar('ContainedType', bound='ContainedCls')
SubContained = TypeVar('SubContained')
T = TypeVar('T')
class Root_wrong(Generic[ContainedType[SubContained]]):
def __init__(self, ct):
# type: (ContainedType[SubContained]) -> None
self.contained = ct
def iter_subcontained(self):
# type: () -> List[SubContained]
return self.contained.items
class ContainedCls(Generic[T]):
def __init__(self, items):
# type: (List[T]) -> None
self.items = items
Because TypeVars can't be indexed.
This also doesn't work, because iter_subcontained can't be typed:
class Root(Generic[ContainedType]):
def __init__(self, ct):
# type: (ContainedType) -> None
# I know because of the bound that ContainedType will have a parameter of its own, but I
# can't refer to it in the `class Root` declaration because TypeVars can't be indexed.
# and the index to Generic has to be a typevar
self.contained = ct
def iter_subcontained(self):
# what goes here?
return self.contained.items
reveal_type(Root(ContainedCls([1])).iter_subcontained()) # Any
Alas, this also doesn't work:
class Root2(Generic[ContainedType, SubContained]):
def __init__(self, ct):
# type: (ContainedType) -> None
# I know because of the bound that ContainedType will have a parameter of its own, but I
# can't refer to it in the `class Root` declaration because TypeVars can't be indexed.
# and the index to Generic has to be a typevar
self.contained = ct
def iter_subcontained(self):
# type: () -> List[SubContained]
return self.contained.items
reveal_type(Root2(ContainedCls([1])).iter_subcontained()) # builtins.list[<nothing>]
# not matching is not an error
reveal_type(Root2[ContainedType[int], str](ContainedCls([1])).iter_subcontained()) # builtins.list[str]
This almost works:
class Root3(Generic[T]):
def __init__(self, ct1, ct2):
# type: (ContainedCls[T], ContainedCls[T]) -> None
self.contained = ct1
self.contained2 = ct2
def iter_subcontained(self):
# type: () -> List[T]
return self.contained.items
class ContainedClsPrime(ContainedCls):
pass
# no way to ensure that ct1 and ct2 are the same container type
# in fact, they aren't even constrained to have the same T!
x = Root3[int](ContainedCls([1]), ContainedClsPrime(['definitely not an int']))
reveal_type(x.iter_subcontained()) # builtins.list[str]
reveal_type(x.contained2.items[0]) # builtins.int* !!!
But 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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue にある 4 つの例を mypy で再現し、明らかになった型を記載された期待値と比較します。インデックス付き TypeVars と高カインド型に関連するジェネリックおよび TypeVar の処理を読みます。ネストされたコンテナ型を保持するための、サポートされた回避策または明確に範囲を定めた実装パスを確立できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 30/100