(3.14) Templates not typeable without TypeVarTuple transformations
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 82
描述
I recently tried to create my own stubs for Python 3.14's Template and Interpolation types, but it seems that is currently not possible. I waited a while and searched a lot and did not find anybody talk about this issue anywhere.
Their runtime generic-ness was recently added:
https://github.com/python/cpython/issues/133970
But it seems that it is not currently possible to actually type hint them correctly within the current typing spec, because transformations of the TypeVarTuple would be necessary. (https://github.com/python/typing/issues/1216)
Currently the code for those two classes' typing stubs looks like this:
class Template: # TODO: consider making `Template` generic on `TypeVarTuple`
strings: tuple[str, ...]
interpolations: tuple[Interpolation, ...]
def __new__(cls, *args: str | Interpolation) -> Template: ...
def __iter__(self) -> Iterator[str | Interpolation]: ...
def __add__(self, other: Template, /) -> Template: ...
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
@property
def values(self) -> tuple[Any, ...]: ... # Tuple of interpolation values, which can have any type
@final
class Interpolation:
value: Any # TODO: consider making `Interpolation` generic in runtime
expression: str
conversion: Literal["a", "r", "s"] | None
format_spec: str
__match_args__ = ("value", "expression", "conversion", "format_spec")
def __new__(
cls, value: Any, expression: str = "", conversion: Literal["a", "r", "s"] | None = None, format_spec: str = ""
) -> Interpolation: ...
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
And it would need to be updated to something like this (using newer syntax for this example):
from types import GenericAlias
from typing import Any, final, Iterator, Literal
class Template[*Ts]:
strings: tuple[str, ...]
interpolations: tuple[Interpolation[Ts], ...] # Not possible to map/transform onto interpolations
def __new__(cls, *args: str | Interpolation[Ts]) -> Template: ... # Not possible to map/transform onto interpolations
def __iter__(self) -> Iterator[str | Interpolation[Ts]]: ... # Not possible to map/transform onto interpolations
def __add__[*OtherTs](self, other: Template[*OtherTs], /) -> Template[*Ts, *OtherTs]: ...
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
@property
def values(self) -> tuple[*Ts]: ...
@final
class Interpolation[T]:
value: T
expression: str
conversion: Literal["a", "r", "s"] | None
format_spec: str
__match_args__ = ("value", "expression", "conversion", "format_spec")
def __new__(
cls, value: T, expression: str = "", conversion: Literal["a", "r", "s"] | None = None, format_spec: str = ""
) -> Interpolation[T]: ...
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
So basically we would need to map the TypeVarTuple's elements into each Interpolation, which is currently not possible.
So it looks like typing Templates and Interpolations is not possible until transformations get added. If I missed something or there is a workaround, please inform me! Thank you! 👍
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先查看 issue 中 Template 和 Interpolation 的 stub 定义,然后阅读所链接的 CPython issue 以及关于 TypeVarTuple 转换的 typing-spec 讨论。确定 typing 规范是否有受支持的变通方案,或者是否需要新的能力;当确定了一种经过认可且可实现的方式来为这两个类添加类型时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100