TypeVarTuple Transformations Before Unpack
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 8
説明
It would be useful to be able to apply type transformations to TypeVarTuple's types. Some motivating examples:
Ts = TypeVarTuple('Ts')
class Example(Generic[*Ts]):
def __init__(self, *types: *type[Ts]) -> None:
self.types = types
def create_defaults(self) -> tuple[*Ts]:
return tuple(t() for t in self.types)
def create_lists(self) -> tuple[*list[Ts]]:
return tuple([t()] for t in self.types)
def create_callables(self) -> tuple[*Callable[[], Ts]]:
return tuple(lambda: t() for t in self.types)
e = Example(int, str)
assert_type(e.create_defaults(), tuple[int, str])
assert_type(e.create_lists(), tuple[list[int], list[str]])
assert_type(e.create_callables(), tuple[Callable[[], int], Callable[[], str]])
At a high level, everything between the * and the TypeVarTuple it expands would be applied per type in the tuple before it's unpacked.
There are some decisions to be made about how the expansion is done if there are multiple TypeVarTuples in the expand expression, but I think it can be supported and I think the intuitive thing would be to zip up all the TypeVarTuples. For example,
tuple[*tuple[Ts, Us]] with Ts = (int, str) and Us = (float, bytes) would be tuple[tuple[int, float], tuple[str, bytes]], with the implementation enforcing equal-length TypeVarTuples.
If there's a workaround for this using the existing logic, let me know. My current use case involves keeping a reference to the runtime types, so *type[Ts] is what I'm looking for at the moment.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
ファイル、テスト、エントリーポイントは指定されていません。まず既存の TypeVarTuple のアンパック動作と動機となる例を確認し、次にアンパック前に変換をどのように適用するかを定義してください。これには、複数の TypeVarTuples に対して提案されている同じ長さの zip セマンティクスも含まれます。機能の動作と、未解決の展開に関する決定が実装できる程度に明確に仕様化されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100