python / python/mypy

Overlapping overload detection fails when signature contains TypeVar

Open
#10,157 1 comment 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

Mypy detects and reports overlapping function overloads that differ in return type.

In the example below, it correctly reports that the second overload of func1 overlaps the first overload. However, when the concrete type str is replaced by a generic type _T, it fails to report the error.

@overload
def func1(
    a: str,
    b: Literal[False] = ...,
) -> List[str]: ...
@overload
def func1(
    a: str,
    b: bool = ...,
) -> List[Union[str, int]]: ...

_T = TypeVar("_T")
@overload
def func2(
    a: _T,
    b: Literal[False] = ...,
) -> List[_T]: ...
@overload
def func2(
    a: _T,
    b: bool = ...,
) -> List[Union[_T, int]]: ...

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 two overload examples from the issue through mypy and compare the diagnostics for func1 and func2. Then trace the overload-overlap detection path responsible for these diagnostics. Done means mypy reports the overlapping overloads in the TypeVar-based func2 example without regressing the concrete case.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.