python / python/mypy

Mypy should ensure overloads are compatible with the implementation

Open
#11,086 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Mypy should ensure overloads are compatible with the implementation

To Reproduce

from typing import Union, overload
from typing_extensions import Literal

@overload
def f(*, flag: Literal[True]) -> float: ...
@overload
def f(*, flag: Literal[False] = False) -> str: ...
def f(*, flag: bool = False) -> Union[str, float]:
    return 5

reveal_type(f(flag=True))
reveal_type(f(flag=False))
reveal_type(f())

Expected Behavior

an error along the lines of "signature 2 incompatible with implementation" because it cannot return a str when flag=False

Actual Behavior

$ mypy test.py 
test.py:11:13: note: Revealed type is "builtins.float"
test.py:12:13: note: Revealed type is "builtins.str"
test.py:13:13: note: Revealed type is "builtins.str"

Your Environment

  • Mypy version used:
$ mypy --version
mypy 0.910

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 provided overload example with mypy 0.910 and confirm the revealed types and missing diagnostic. Trace the overload compatibility checks for the implementation signature; done means incompatible overloads, such as the false-flag case returning str, produce an error without rejecting compatible overloads.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.