python / python/typing

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

Ouverte
#2,211 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

topic: conformance tests
Langage dominant
Python
Étoiles
1.8k
Forks
302
Merge moyen
23 h
PR mergées (30 j)
8

Description

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?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par conformance/tests/generics_defaults.py aux lignes 191-200 et comparez son exigence avec la spécification de typing. Examinez ensuite typing.py dans _collect_type_parameters et reproduisez les exemples dans les versions de syntaxe Python citées. Le travail est terminé lorsqu’il est déterminé si le runtime ou la spécification et le test de conformance sont incohérents, avec une couverture de régression pour la correction choisie.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
documentation, testing
Type d'issue
Bug
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.