python / python/typing

`generics_defaults.py` mandates that type checkers should allow a class that raises `TypeError` at runtime

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

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

topic: conformance tests
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

This test mandates that type checkers should allow classes that have ParamSpecs-with-defaults following TypeVarTuples: https://github.com/python/typing/blob/22d9392a5ead302cc02c47ecbbcf3aa40106fd9f/conformance/tests/generics_defaults.py#L191-L200

But these fail at runtime. Using Python 3.11 syntax:

% uvx python3.14       
Python 3.14.0 (main, Oct 10 2025, 12:54:13) [Clang 20.1.4 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import *
>>> Ts = TypeVarTuple("Ts")
>>> P = ParamSpec("P", default=[int, str])
>>> class A(Generic[*Ts, P]): ...
... 
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    class A(Generic[*Ts, P]): ...
            ~~~~~~~^^^^^^^^
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 402, in inner
    return func(*args, **kwds)
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 1157, in _generic_class_getitem
    return _GenericAlias(cls, args)
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 1334, in __init__
    self.__parameters__ = _collect_type_parameters(
                          ~~~~~~~~~~~~~~~~~~~~~~~~^
        args,
        ^^^^^
        enforce_default_ordering=enforce_default_ordering,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 291, in _collect_type_parameters
    raise TypeError('Type parameter with a default'
                    ' follows TypeVarTuple')
TypeError: Type parameter with a default follows TypeVarTuple

Using Python <=3.10 syntax:

>>> class B(Generic[Unpack[Ts], P]): ...
... 
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    class B(Generic[Unpack[Ts], P]): ...
            ~~~~~~~^^^^^^^^^^^^^^^
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 402, in inner
    return func(*args, **kwds)
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 1157, in _generic_class_getitem
    return _GenericAlias(cls, args)
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 1334, in __init__
    self.__parameters__ = _collect_type_parameters(
                          ~~~~~~~~~~~~~~~~~~~~~~~~^
        args,
        ^^^^^
        enforce_default_ordering=enforce_default_ordering,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 291, in _collect_type_parameters
    raise TypeError('Type parameter with a default'
                    ' follows TypeVarTuple')
TypeError: Type parameter with a default follows TypeVarTuple

And using Python 3.13+ syntax:

>>> class C[*Ts, **P = [int, str]]: ...
... 
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    class C[*Ts, **P = [int, str]]: ...
  File "<python-input-5>", line 1, in <generic parameters of C>
    class C[*Ts, **P = [int, str]]: ...
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 1334, in __init__
    self.__parameters__ = _collect_type_parameters(
                          ~~~~~~~~~~~~~~~~~~~~~~~~^
        args,
        ^^^^^
        enforce_default_ordering=enforce_default_ordering,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/alexw/Library/Application Support/uv/python/cpython-3.14.0-macos-aarch64-none/lib/python3.14/typing.py", line 291, in _collect_type_parameters
    raise TypeError('Type parameter with a default'
                    ' follows TypeVarTuple')
TypeError: Type parameter with a default follows TypeVarTuple

Since it seems that the tests are quoting the spec here, is this a bug in the runtime implementation of the typing module? Or should we update the spec and the conformance suite to allow type checkers to catch this runtime error?

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

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

conformance/tests/generics_defaults.py の191-200行から始め、その要件を typing 仕様と比較します。次に typing.py の _collect_type_parameters を調査し、引用されている Python 構文バージョン全体で例を再現します。完了条件は、runtime と仕様および conformance test のどちらが不整合なのかを判断し、選択した修正に対する回帰テストのカバレッジを追加することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation, testing
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。