Skip to content

No type narrowing when isinstance list comprehension is assigned to the same variable #19528

@injust

Description

@injust

Bug Report

When a list comprehension with an isinstance condition is assigned back to the original list variable, its type is not narrowed.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&gist=202edc6034b8e5d983a0fb90167bf5ee

class Foo:
    pass


class Bar(Foo):
    pass


def test(things: list[Foo]) -> None:
    things = [thing for thing in things if isinstance(thing, Bar)]
    reveal_type(things)

Expected Behavior

things should be type narrowed from list[Foo] to list[Bar].

Actual Behavior

main.py:11: note: Revealed type is "builtins.list[__main__.Foo]"
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.17.0
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions