python / python/mypy

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

Offen
#17,702 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verlinkten mypy-play-Reproduktion und dem overload-Beispiel im Issue und verfolge anschließend, wie mypy sich überschneidende overload-Signaturen prüft, die Self und Basisklassen betreffen. Das Ziel ist erreicht, wenn das Beispiel keinen False-Positive-Fehler meldet und gleichzeitig die vier im Bericht gezeigten aufgedeckten Typen beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.