python / python/mypy

Generic[T_co] erroring when T_co = TypeVar("T_co", A, B, covariant=True) and passed subclass of A

Aperta
#8,806 9 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-error-reporting topic-usability
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

  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 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.