Skip to content

Inconsistent Any matching with Overloads in TypeGuard/TypeIs #18659

Open
@davidhalter

Description

@davidhalter

Bug Report

There is an inconsistency left where currently TypeIs/TypeGuard narrows in case of multi-Any matches, while this is not done in normal overloads. I find this strange and would prefer that it is consistent.

To Reproduce

@overload
def func1(x: str) -> TypeIs[str]:
    ...

@overload
def func1(x: int) -> TypeIs[int]:
    ...

def func1(x: Any) -> Any:
    return True

def func2(val: Any):
    if func1(val):
        reveal_type(val)  # Currently int | str


@overload
def func3(x: str) -> str:
    ...

@overload
def func3(x: int) -> int:
    ...

def func3(x: Any) -> Any:
    return True

def func4(val: Any):
    reveal_type(func3(val))  # Currently Any

Playground (used Mypy 1.15)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions