python / python/typing

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

Aperta
#2,211 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: conformance tests
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

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?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con conformance/tests/generics_defaults.py alle righe 191-200 e confronta il suo requisito con la specifica di typing. Poi esamina typing.py in _collect_type_parameters e riproduci gli esempi nelle versioni della sintassi Python citate. Il lavoro è completato quando si è deciso se il runtime oppure la specifica e il test di conformance sono incoerenti, con una copertura di regressione per la correzione scelta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation, testing
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.