python / python/mypy

Incorrect has-type on unpacking of known TypeVarTuple type from classmethod from class with contrained generic

Aperta
#21,693 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

When trying to unpack a tuple of a TypeVarTuple from a class method where that class contains a constrained TypeVar different from the one inhabited by the TypeVarTuple, mypy will issue a has-type error despite reveal_type showing the full correct type.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.14&gist=856510a8671b57985553a6439ee673dc

class Container[T, P: (Literal[True], Literal[False])]:
    def foo(self) -> T:
        raise NotImplementedError

def f[*Ts, P: (Literal[True], Literal[False])](
    container: Container[tuple[*Ts], P],
) -> None:
    result = container.foo()
    reveal_type(result)  # Revealed type is "tuple[*Ts]"
    y = (*result,)  # Cannot determine type of "result"  [has-type]
    # Expected: tuple[*Ts]
    # Actual: tuple[Any, ...]
    reveal_type(y)

Expected Behavior

This code should have no errors, and the type of y should be tuple[*Ts]

Actual Behavior

Despite the reveal_type of result clearly showing tuple[*Ts], mypy issues a has-type error when unpacking it, and the resulting type of y ends up being wrong.

Additional Notes

If you remove the P generic, or make it normal/bound instead of constrained, the code works as expected.

Adding an annotation to result does get rid of the error, however in my actual code the result from foo is a tuple that I unpack like this result, index = container.foo(), so I cannot easily add that annotation. (I can like this result: tuple[*Ts] before the unpacking assignment, but that makes the code uglier.)

Your Environment

  • Mypy version used: Playground 2.1.0 / master
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.14

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

Inizia con il riproduttore mypy-play collegato e confronta il caso di TypeVar vincolato con le varianti funzionanti che rimuovono o modificano P. Traccia come viene gestito il tuple[*Ts] rivelato durante l'unpacking con asterisco. Il lavoro è completato quando il riproduttore non produce alcun errore has-type e y viene inferito come tuple[*Ts], con un test di regressione che copra il caso.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.