python / python/mypy

Mypy is still too eager about outer context

Aperta
#5,874 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug false-positive priority-1-normal topic-type-variables topic-union-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Consider this example:

T = TypeVar('T')
S = TypeVar('S')

def add(x: List[T], y: List[S]) -> List[Union[T, S]]: ...

a: List[int]
b: List[str]

e: List[Union[int, str]] = add(a, b)

It fails on the last line with the following (I skip invariance notes):

error: Argument 1 to "add" has incompatible type "List[int]"; expected "List[Union[int, str]]"
error: Argument 2 to "add" has incompatible type "List[str]"; expected "List[Union[int, str]]"

If I remove the annotation for e, everything works fine.

When previously working on the external context issues, I found that I don't like how constrains for unions against unions are inferred (not that they are wrong, just counterintuitive). For example, Union[T, S] <: Union[str, int] infers T < : Union[str, int] and S <: Union[str, int], which is technically fine, but leads to errors like above.

This blocks https://github.com/python/mypy/pull/5873

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 l’esempio annotato add(a, b) con mypy e leggi la discussione relativa alla pull request 5873 indicata. Determina l’inferenza dei vincoli prevista per le union rispetto alle union, quindi verifica che l’esempio abbia esito positivo senza introdurre regressioni.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.