Unexpected Errors When Using Invariant, Covariant, and Contravariant Type Variables in Protocols for `__class_getitem__`
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
I encountered an issue while defining a protocol with type variables that are invariant, covariant, and contravariant for the __class_getitem__ method. My goal was to create a protocol that supports different variance for the key parameter. I expected that at least one of the variance options (invariant, covariant, or contravariant) would work without errors. However, all three approaches result in type errors:
- Invariant Type Variable: Using an invariant type variable
Tresults in an error because the protocol expects a covariant type variable in this context. - Covariant Type Variable: When using a covariant type variable
T_co, I cannot use it as a parameter for the__class_getitem__method, which expects invariance or contravariance. - Contravariant Type Variable: Using a contravariant type variable
T_contraalso leads to an error since it is expected to be covariant in this context.
I was expecting that at least one of these cases would not raise an error.
To Reproduce
from abc import abstractmethod
from typing import Protocol, Self, TypeVar
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
T_contra = TypeVar("T_contra", contravariant=True)
R = TypeVar("R")
R_co = TypeVar("R_co", covariant=True)
R_contra = TypeVar("R_contra", contravariant=True)
class SupportsClassGetItemInvariant(Protocol[T, R_co]):
@classmethod
@abstractmethod
def __class_getitem__(cls: type[Self], key: T, /) -> R_co: ...
class SupportsClassGetItemCovariant(Protocol[T_co, R_co]):
@classmethod
@abstractmethod
def __class_getitem__(cls: type[Self], key: T_co, /) -> R_co: ...
class SupportsClassGetItemContravariant(Protocol[T_contra, R_co]):
@classmethod
@abstractmethod
def __class_getitem__(cls: type[Self], key: T_contra, /) -> R_co: ...
Expected Behavior
I expected that at least one of these variations (invariant, covariant, or contravariant) would work without causing a type error in Mypy, allowing me to use the type variable as the key parameter in the __class_getitem__ method.
Actual Behavior
Mypy raises errors for all three cases, indicating that none of the variance annotations are accepted for the key parameter in the __class_getitem__ method within the protocol.
Your Environment
- Mypy version used:
mypy 1.11.2 - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini: None - Python version used:
Python 3.12.2
I would appreciate any insights or suggestions on how to approach this issue, as I was expecting at least one of these scenarios to work.
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
Riproduci le tre definizioni di protocollo dell'issue con Python 3.12.2 e mypy 1.11.2, quindi confronta le diagnosi di varianza riportate. Esamina la gestione della varianza dei protocolli da parte di mypy e i test esistenti per class_getitem; il lavoro è completato quando è stato stabilito il comportamento di varianza valido previsto e viene aggiunta una copertura di regressione per questi casi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100