python / python/typing

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

未关闭
#2,211 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: conformance tests
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。