TypeVarTuple Transformations Before Unpack
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 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 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
未指定文件、测试或入口点。首先审查现有的 TypeVarTuple 解包行为和动机示例,然后定义转换如何在解包之前应用,包括针对多个 TypeVarTuples 提议的等长度 zip 语义。当该功能的行为以及尚未解决的展开决策都被明确规范到足以实现时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100