python / python/mypy

Weird error message for incompatible override when using a `TypeVar` for `self`

Offen
#12,786 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-error-reporting topic-inheritance topic-self-types topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Minimal repro:

from typing import TypeVar

Self = TypeVar("Self")

class Flag:
    def __or__(self: Self, other: Self) -> Self: ...
    __ror__ = __or__

class IntFlag(int, Flag):
    def __or__(self: Self, other: int) -> Self: ...
    def __ror__(self: Self, other: int) -> Self: ...

mypy output:

main.py:11: error: Signature of "__ror__" incompatible with supertype "Flag"
main.py:11: note:      Superclass:
main.py:11: note:          def [Self] __or__(self, Self, Self) -> Self
main.py:11: note:      Subclass:
main.py:11: note:          def __ror__(self, int) -> IntFlag

Mypy playground link here: https://mypy-play.net/?mypy=latest&python=3.10&gist=26b60881cd02e608bb9e555db4c3f627

Expected Behavior

I'm not sure an error is even appropriate here: no incompatible override is reported for __or__.

Even if an error is appropriate here, however, the error message is weird and garbled. According to the error message, mypy seems to think that __ror__ in the superclass expects three arguments (it only expects two). It also seems to think the method in the subclass will return IntFlag (not necessarily true; the return type will be whatever self is, and self could be any subtype of IntFlag).

The function that creates this error message is here: https://github.com/python/mypy/blob/9b4bce9065cbef6185fbc77f2849b63dc9e5e293/mypy/messages.py#L824

Mypy version used

  • 0.950.
  • This doesn't seem to be a recent regression: I can reproduce as far back as 0.920, which is the earliest mypy version that has the verbose error messages.
  • I've also reproduced the bug on mypy master.

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 minimalen Python-Reproduktion und untersuche die Erstellung der Diagnose in mypy/messages.py um Zeile 824. Vergleiche die gemeldeten Signaturen der Ober- und Unterklasse mit den auf TypeVar basierenden Self-Typen und bestimme anschließend, ob das Überschreiben akzeptiert werden sollte oder wie seine Meldung dargestellt werden sollte. Als erledigt gilt die Aufgabe, wenn das Verhalten korrigiert und die Reproduktion durch einen Regressionstest abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
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.