No error reported when a contravariant TypeVar is used as a return type within a tuple
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Mypy reports an error (as expected) when a contravariant TypeVar is used as a return type. However, if it is wrapped in a tuple, no error is reported (unexpected, I think). The same issue is present when using list here instead of tuple.
This issue is not present when using the 3.12+ type parameter syntax (i.e., the variance is inferred correctly as not-contravariant).
To Reproduce
from typing import TypeVar
T = TypeVar("T", contravariant=True)
def f(x: T) -> tuple[T]:
return (x,)
Expected Behavior
Mypy reports an error.
Actual Behavior
No error reported.
A longer reproducible example
- gist: https://gist.github.com/mypy-play/489aaad37715e215ecab49ee128553d5
- playground: https://mypy-play.net/?mypy=latest&python=3.13&flags=strict&gist=489aaad37715e215ecab49ee128553d5
This covers the contravariant variable being (i) used properly, (ii) used as output directly, (iii) wrapped in a tuple in the output, under the old and new typing syntaxes.
Environment
- Mypy version used: mypy 1.18.2 (compiled: yes)
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: Python 3.13.8
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore minimo nell'issue ed eseguilo con le impostazioni --strict di mypy, confrontando i ritorni diretti, di tuple e di list con i casi di parametri di tipo di Python 3.12+ descritti lì. Il lavoro è completato quando mypy segnala l'errore previsto per il TypeVar controvariante in ogni forma di ritorno applicabile, preservando al contempo il comportamento della sintassi più recente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100