false positive: "Overloaded function signatures ... overlap with incompatible return types"
Open
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
Code of a certain form causes a false positive error:
error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
To Reproduce
Gist: mypy-play.net
from typing import (
Literal,
overload,
Sequence,
TypeVar,
Union,
)
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@overload
def func(a1: Literal[True], a2: T1) -> Union[T1, Sequence[T2]]: ...
# error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
@overload
def func(a1: bool, a2: T1) -> Union[T1, Sequence[T2], T2]: ...
def func( # type: ignore[empty-body]
a1: bool, a2: T1) -> Union[T1, Sequence[T2], T2]:
...
Expected Behavior
There should be no errors.
- the second signature's arguments are clearly broader than the first's (
boolvs.Literal[True]) - the second signature's return type is clearly broader than the first's (
Union[...]vs.Union[..., T2])
Actual Behavior
An error is issued:
error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
Additional notes
This example is fairly minimal:
- removing the
a2argument from all three declarations causes the problem to disappear - removing the
Sequence[T2]clause from the returnUnionof all three declarations causes the problem to disappear
Your Environment
- Mypy version used: 0.910, 1.8.0
- Mypy command-line flags: (none)
- Mypy configuration options from
mypy.ini(and other config files): (none) - Python version used: 3.8, 3.12
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked mypy-play reproducer and narrowing it using the minimal variants described in the issue. No source file or test entry point is identified; done means the example and its stated variants no longer produce the false-positive overload error, with regression coverage added in the appropriate mypy test location.
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
- Needs clarification
- Newbie friendliness
- 35/100