Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Viicos opened this issue Oct 21, 2024 · 1 comment · May be fixed by #18193
Open

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

Viicos opened this issue Oct 21, 2024 · 1 comment · May be fixed by #18193
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@Viicos
Copy link
Contributor

Viicos commented Oct 21, 2024

playground

from typing import Any, Callable
from typing_extensions import TypeIs, assert_type, assert_never


def is_single_callable(c: Callable[[], Any] | Callable[[int], Any]) -> TypeIs[Callable[[int], Any]]:
    ...


def test(c: Callable[[], Any] | Callable[[int], Any]):
    if is_single_callable(c):
        reveal_type(c)
    else:
        # mypy thinks this is unreachable
        a: 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
@Viicos Viicos added the bug mypy got something wrong label Oct 21, 2024
@brianschubert
Copy link
Collaborator

It turns out this affects any union of a generic type (not just Callable). For example:

def is_int_list(c: list[str] | list[int]) -> TypeIs[list[int]]:
    ...

def foo(x: list[str] | list[int]) -> None:
    if is_int_list(x):
        reveal_type(x)  # N: Revealed type is "builtins.list[builtins.int]"
    else:
        reveal_type(x)  # E: Statement is unreachable
    reveal_type(x)      # N: Revealed type is "builtins.list[builtins.int]"

@brianschubert brianschubert added the topic-type-narrowing Conditional type narrowing / binder label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants