python / python/mypy

Inconsistent Any matching with Overloads in TypeGuard/TypeIs

Open
#18,659 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked Playground reproduction with mypy 1.15 or the latest version, comparing TypeIs/TypeGuard narrowing with ordinary overload resolution. Trace the two type-checking paths shown by the reveal_type results and add regression coverage for the intended consistent behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.