python / python/mypy

TypeVarTuple instantiation with the empty tuple in Generic not being recognized as such

Offen
#19,533 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-pep-646
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Mypy does not recognize that a TypeVarTuple was instantiated with the empty tuple in a generic.

To Reproduce

from typing import Generic, reveal_type, TypeVarTuple

Ts = TypeVarTuple('Ts')

class A(Generic[*Ts]):

    def args(self) -> tuple[*Ts]:
        raise NotImplementedError

class B(A[int, *Ts]):
    pass

class C(B[*tuple[()]]):
    pass

reveal_type(A.args)
reveal_type(B.args)
reveal_type(C.args)

Expected Behavior

I'd expect that the return type of C.args be tuple[int]. And this, indeed, is what pyright infers:

test.py:18:13 - information: Type of "A.args" is "(self: A[Unknown]) -> tuple[Unknown]"
test.py:19:13 - information: Type of "B.args" is "(self: B[Unknown]) -> tuple[int, Unknown]"
test.py:20:13 - information: Type of "C.args" is "(self: C) -> tuple[int]"

Actual Behavior

Mypy however infers tuple[int, *tuple[Any, ...]]:

test.py:18: note: Revealed type is "def [Ts] (self: test8.A[Unpack[Ts`1]]) -> tuple[Unpack[Ts`1]]"
test.py:19: note: Revealed type is "def [Ts] (self: test8.A[builtins.int, Unpack[Ts`1]]) -> tuple[builtins.int, Unpack[Ts`1]]"
test.py:20: note: Revealed type is "def (self: test8.A[builtins.int, Unpack[builtins.tuple[Any, ...]]]) -> tuple[builtins.int, Unpack[builtins.tuple[Any, ...]]]"

Your Environment

  • Mypy version used: 1.17.0 (compiled: yes)
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.13.5

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Beispiel in test.py mit mypy 1.17.0 und untersuche die Behandlung von TypeVarTuple im Zusammenhang mit der Vererbung von Generic und der Instanziierung eines leeren Tupels. Füge einen Regressionstest auf Grundlage des Beispiels hinzu, wobei durch die Vervollständigung gezeigt wird, dass C.args als tuple[int] statt als ein unbeschränktes Tupel zurückgegeben wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.