python / python/mypy

False positive: "overloaded signature will never be matched" with Self

Open
#17,702 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When combining Self and base class overloads, mypy incorrectly warns about the former being "the same or broader" as the latter. However, those overloads are both actually used.

I do know that this can be solved with a single TypeVar - the case is extracted from a more complicated code where similar TypeVar solution doesn't seem to apply.

To Reproduce

playground

from __future__ import annotations

from typing import Self, overload

class Base:
    @overload
    def fn(self, other: Self) -> Self: ...
    @overload
    def fn(self, other: Base) -> Base: ...
    def fn(self, other: Base) -> Base:
        return other


class X(Base): ...
class Y(Base): ...

reveal_type(X().fn(X()))
reveal_type(X().fn(Y()))
reveal_type(Y().fn(X()))
reveal_type(Y().fn(Y()))

Expected Behavior

No errors reported

Actual Behavior

main.py:10: error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader  [misc]
main.py:18: note: Revealed type is "__main__.X"
main.py:19: note: Revealed type is "__main__.Base"
main.py:20: note: Revealed type is "__main__.Base"
main.py:21: note: Revealed type is "__main__.Y"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.1 and master (playground)
  • Mypy command-line flags: none and --strict - both reproduce
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11, 3.12 - does not affect

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 with the linked mypy-play reproduction and the overload example in the issue, then trace how mypy checks overlapping overload signatures involving Self and base classes. Done means the example reports no false-positive error while preserving the four revealed types shown in the report.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.