mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Type narrowing fails for union of iterable and dictionary when using `isinstance(x, Iterable)` in a conditional

Open zigaLuksic opened this issue 3 years ago • 0 comments

Bug Report

When using Union[Iterable[int], Dict[str, int]] together with isinstance(x, Iterable) in a conditional, the narrowing of the type does not work as expected. The previously correctly narrowed type is reverted to the un-narrowed type inside the branch.

To Reproduce

from typing import Dict, Iterable, Union


def test_iterable(x: Union[Iterable[int], Dict[str, int]]) -> Iterable[int]:
    if isinstance(x, dict):
        return [1, 2, 3]
    reveal_type(x)  # typing.Iterable[builtins.int]
    if isinstance(x, Iterable):
        reveal_type(x)  # Union[typing.Iterable[builtins.int], builtins.dict[builtins.str, builtins.int]]
        return x  # mypy falsely returns an error here

Expected Behavior

The type of x should be correctly narrowed down to Iterable[int].

Actual Behavior

Running mypy returns:

example.py:10: error: Incompatible return value type (got "Union[Iterable[int], Dict[str, int]]", expected "Iterable[int]")
Found 1 error in 1 file (checked 1 source file)

The problem persists even if the second if is changed to elif.

Your Environment

I used a clean virtual environment to test, only mypy was installed.

  • Mypy version used: 0.971
  • Python version used: 3.9.5

zigaLuksic avatar Sep 22 '22 20:09 zigaLuksic