Skip to content

Type narrowed to Never when using TypeIs with union of callables #18009

Description

@Viicos

playground

fromtypingimportAny, Callablefromtyping_extensionsimportTypeIs, assert_type, assert_neverdefis_single_callable(c: Callable[[], Any] |Callable[[int], Any]) ->TypeIs[Callable[[int], Any]]:
...
deftest(c: Callable[[], Any] |Callable[[int], Any]):
ifis_single_callable(c):
reveal_type(c)
else:
# mypy thinks this is unreachablea: int=1

While c could be narrowed to Callable[[], Any] in the else block, it is currently narrowed to Never. For reference, see this discussion.

Your Environment

  • Mypy version used: 1.12.1
  • Mypy command-line flags: --warn-unreachable
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / bindertopic-typeguard-typeisTypeGuard / TypeIs / PEP 647 / PEP 742

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions