python / python/mypy

False positive list comprehension for union type source iterable

Open
#17,907 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided TypeVar and union-iterable reproducer with mypy 1.11.2, then trace list-comprehension type inference and the handling of union sources. Add regression coverage showing that the comprehension preserves the union of list types, including the TypeVar case, and verify the reported assignment error is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.