No error reported when a contravariant TypeVar is used as a return type within a tuple
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem minimalen Reproducer im Issue und führe ihn mit mypy's --strict-Einstellungen aus. Vergleiche dabei direkte, tuple- und list-Rückgaben mit den dort beschriebenen Type-Parameter-Fällen von Python 3.12+. Als erledigt gilt die Aufgabe, wenn mypy für jede zutreffende Rückgabeform den erwarteten Fehler für den kontravarianten TypeVar meldet und dabei das Verhalten der neueren Syntax beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100