python / python/cpython

[PEP 696] `TypeError` when subclassing and omitting `TypeVar` with default.

未關閉
#140,596 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib topic-typing type-bug
主要語言
Python
星號
77.2k
分支
36k
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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 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

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。