[PEP 696] `TypeError` when subclassing and omitting `TypeVar` with default.
未关闭
还没有人认领这个 Issue。
stdlib
topic-typing
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
The following example raises a TypeError: Some type variables (T) are not listed in Generic[U]:
class Bar[T, S=T]: ...
class Baz[U](Bar[U]): ...
Full Traceback
Traceback (most recent call last):
File "/home/rscholz/Downloads/tmp.py", line 3, in <module>
class Baz[U](Bar[U]): ...
File "/home/rscholz/Downloads/tmp.py", line 3, in <generic parameters of Baz>
class Baz[U](Bar[U]): ...
File "/usr/lib/python3.13/typing.py", line 1288, in _generic_init_subclass
raise TypeError(f"Some type variables ({s_vars}) are"
f" not listed in Generic[{s_args}]")
TypeError: Some type variables (T) are not listed in Generic[U]
The PEP states that
non-overridden defaults should be substituted in, and type parameters with such defaults can be further specialised down the line.
And the typing spec uses similar phrasing, which makes me believe this is a bug. In the example above, I'd expect that Baz[U] is a Bar[U, U] by substitution + default.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-155646
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 /usr/lib/python3.13/typing.py 中的 _generic_init_subclass 开始,这里会引发所报告的 TypeError,并将其行为与 PEP 696 以及 typing 规范中关于子类化的章节进行比较。完成的标准是:所示的 Bar/Baz 示例遵循文档所述的默认值替换行为,且不会引发所报告的错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 20/100