python / python/mypy

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

Aperta
#19,533 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-pep-646
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

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

Riprodurre l’esempio in test.py con mypy 1.17.0 e analizzare la gestione di TypeVarTuple coinvolta nell’ereditarietà di Generic e nell’istanza di una tupla vuota. Aggiungere un test di regressione basato sull’esempio, mostrando tramite il completamento che C.args viene rivelato come restituente tuple[int] invece di includere una tupla non vincolata.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.