Skip to content

False positive [unreachable] with TypeIs #17181

Open
@sterliakov

Description

@sterliakov

Bug Report

Applying TypeIs to narrow type of an iterable results in too optimistic reachability analysis.

To Reproduce

from collections.abc import Iterable
from typing_extensions import TypeIs

def is_iterable_int(val: Iterable[object]) -> TypeIs[Iterable[int]]:
    return all(isinstance(item, int) for item in val)

    
bar: list[int] | list[str]

if is_iterable_int(bar):
    reveal_type(bar)
else:
    reveal_type(bar)

playground has more code, including TypeGuard comparison and non-iterable case that works correctly.

Expected Behavior

I'd expected both branches to be reachable and to narrow type according to the spec: list[int] in if and list[str] in else, ideally.

Actual Behavior

main.py:11: note: Revealed type is "builtins.list[builtins.int]"
main.py:13: error: Statement is unreachable  [unreachable]

Your Environment

  • Mypy version used: 1.10.0 and master (playground)
  • Mypy command-line flags: --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.11

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