python / python/mypy

false positive: "Overloaded function signatures ... overlap with incompatible return types"

Open
#11,165 5 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

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 (bool vs. 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 a2 argument from all three declarations causes the problem to disappear
  • removing the Sequence[T2] clause from the return Union of 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.