Skip to content

[1.20 regression] mypy no longer detects variable type narrowing via reassignment #21273

@abrahammurciano

Description

@abrahammurciano

Bug Report

Mypy v1.20.0 no longer detects variable type narrowing via reassignment in this case.

To Reproduce

from typing import Iterable

def foo(args: Iterable[str | int] | str | int) -> Iterable[str]:
    if isinstance(args, (str, int)):
        args = (args,)
    args = (
        arg if isinstance(arg, str) else str(arg)
        for arg in args
    )
    return args

Expected Behavior

I expect this code to type check successfully with mypy 1.20.0 like it did with 1.19.1

Actual Behavior

main.py:10: error: Incompatible return value type (got "Generator[str | int, None, None]", expected "Iterable[str]")  [return-value]

Your Environment

  • Mypy version used: 1.19.1 and 1.20.0
  • Python version used: 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions