`generics_defaults.py` mandates that type checkers should allow a class that raises `TypeError` at runtime
還沒有人認領這個 Issue。
- #2310 來自 @ashishpatel26 —— 已關閉,未合併
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- python
- 領域
- documentation, testing
研究方向
從 conformance/tests/generics_defaults.py 的第 191-200 行開始,將其要求與 typing 規範進行比較。接著檢查 typing.py 中的 _collect_type_parameters,並在所引用的 Python 語法版本中重現這些範例。完成的標準是判定 runtime 與規範和 conformance test 哪一方不一致,並為所選擇的修正加入回歸覆蓋。
由索引模型根據 Issue 內容生成。
描述
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?
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 38 分鐘
- 30 天內合併 PR
- 6
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
-
Update reference/generics.html with PEP695 type parameter syntax可能已有人在做 @BHUVANSH855 於 181 天前認領。 未關閉topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
-
Finals and Literals: Spec contradiction可能已有人在做 @srittau 於 34 天前認領。 未關閉topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
json_params_matcher fails on falsy top-level JSON primitives (0, False, "")可能已有人在做 @mayureshsonawane17 今天認領。 未關閉Waiting for: Product Owner
難度 2/5 1-3 小時 新手友好度 84/100
維護者通常 5 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 88/100
bojieli/ai-agent-book#1169 ·
維護者通常 1 天內回覆
-
priority:low ready-for-dev
難度 2/5 1-3 小時 新手友好度 78/100
OpenHands/extensions#738 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100
micronaut-projects/micronaut-core#13677 ·
維護者通常 1 天內回覆