python / python/mypy

False positive list comprehension for union type source iterable

Offen
#17,907 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den bereitgestellten TypeVar- und union-iterable-Reproducer mit mypy 1.11.2 auszuführen; verfolge anschließend die Typinferenz von List Comprehensions und die Behandlung von Union-Quellen. Füge Regressionstestabdeckung hinzu, die zeigt, dass die Comprehension die Union der Listentypen beibehält, einschließlich des TypeVar-Falls, und verifiziere, dass der gemeldete Zuweisungsfehler behoben ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.