python / python/mypy

False positive list comprehension for union type source iterable

Aperta
#17,907 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

MyPy incorrectly asses the type of list comprehensions built from union types. I can't see a way to make this pass typing checks. This and it even affects TypeVar. My only option is a # type: ignore.

The problem arises where a variable foo has a type of Iterable[Bar] | Iterable[Baz] and this is then used as part of a list comprehension [v for v in foo]. MyPy incorrectly evaluates the list comprehension as list[Bar | Baz] not list[Bar] | list[Baz].

I had thought passing a TypeVar might be a simple work around, but it appears the TypeVar is stripped off and only it's binding is maintained.

To Reproduce

I ran into this with code like this:

from typing import TypeVar

class Foo:
    item_id: str

class Bar:
    item_id: str

class FooWrapper:
    items: list[Foo]

class BarWrapper:
    items: list[Bar]


_T = TypeVar('_T', bound=FooWrapper | BarWrapper)


def filter_this(value: _T, filter_val: str) -> None:
    # Mypy Error on this line 👇
    value.items = [item for item in value.items if item.item_id == filter_val]

With the MyPy error:

error: Incompatible types in assignment (expression has type "list[Foo | Bar]", variable has type "list[Foo] | list[Bar]")  [assignment]
Found 1 error in 1 file (checked 1 source file)
Expected Behavior

A list comprehension formed from iterable Iterable[Foo] | Iterable[Bar] should have type list[Foo] | list[Bar]

Actual Behavior

A list comprehension formed from iterable Iterable[Foo] | Iterable[Bar] is assessed as having type list[Foo | Bar]

Your Environment
  • Mypy version used: mypy 1.11.2 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: python 3.11

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 eseguendo il riproduttore fornito di TypeVar e union-iterable con mypy 1.11.2, quindi traccia l’inferenza dei tipi delle list comprehension e la gestione delle sorgenti union. Aggiungi una copertura di regressione che dimostri che la comprehension preserva l’unione dei tipi di lista, incluso il caso TypeVar, e verifica che l’errore di assegnazione segnalato sia risolto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.