python / python/cpython

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

オープン
#140,596 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。