Generic[T_co] erroring when T_co = TypeVar("T_co", A, B, covariant=True) and passed subclass of A
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
I am reporting what I believe is a bug in mypy's interpretation of TypeVar when passed two constraining types and also covariant=True.
I think the below code sample is valid (per Liskov Substitution Principle), and is erroneously being reported as a type error by mypy. Here is my working example:
from typing import TypeVar, Generic, Union
class A:
pass
class ASub(A):
pass
class B:
pass
# Case 1... Success: no issues found
# T_co = TypeVar("T_co", covariant=True)
# Case 2... error: Value of type variable "T_co" of "SomeGeneric" cannot be "ASub" [type-var]
T_co = TypeVar("T_co", A, B, covariant=True)
# Case 3... Success: no issues found
# T_co = TypeVar("T_co", bound=Union[A, B], covariant=True)
class SomeGeneric(Generic[T_co]):
pass
class SomeGenericASub(SomeGeneric[ASub]):
pass
Potentially Related Issue
I may be re-asking the contents of https://github.com/python/mypy/issues/8611, but to be honest, I get confused by the core of the issue
non-empty covariant generic subclass of a contravariant base
Versions
python==3.6.5
mypy==0.770
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 riproducendo il Case 2 segnalato con Python 3.6.5 e mypy 0.770, quindi confronta il comportamento del suo TypeVar vincolato con gli esempi non vincolato e bound. Leggi la discussione potenzialmente correlata in issue #8611; il lavoro è concluso quando il comportamento previsto per SomeGeneric[ASub] è stato stabilito e la diagnostica errata è stata corretta o spiegata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100