mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open sterliakov opened this issue 1 year ago • 0 comments

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

sterliakov avatar Aug 22 '24 11:08 sterliakov