python / python/mypy

Signatures of "__ior__" and "__or__" are incompatible when assigning __or__ with compatible method

Offen
#11,941 1 Kommentar 1 Reaktion 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

Dear mypy team,

I was implementing arithmetic operations on a custom class and ran into the following issue:
When I don't define __or__ in the class body, but assign it with a function with a compatible type, mypy complains about incompatible signatures of __ior__ and __or__. (This is also true for other arithmetic dunders, see example).

I wanted to only allow inplace operations on that class and throw a common error on non-inplace operations.

To Reproduce

# this works and does not throw an error
class A:

    def __or__(self, v: int) -> A:...

    def __ior__(self, v: int) -> None: ...

    reveal_type(__or__)  # note: Revealed type is "def (_error.A, builtins.int) -> _error.A"
    reveal_type(__ior__)  # note: Revealed type is "def (_error.A, builtins.int)"


# this errors
class B:

    def _no(self, v: int) -> B: ...

    __or__ = _no

    def __ior__(self, v: int) -> None: ...  # error: Signatures of "__ior__" and "__or__" are incompatible

    reveal_type(__or__)  # note: Revealed type is "def (self: _error.B, v: builtins.int) -> _error.B"
    reveal_type(__ior__)  # note: Revealed type is "def (_error.B, builtins.int)"

NOTE: the same is true for __and__, __xor__, __add__, __sub__, __mul__, __truediv__, __floordiv__. (I haven't tested others.

Your Environment

  • Mypy version used: 0.931
  • Python version used: python3.9

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 damit, den gemeldeten Unterschied zwischen dem Definieren von or in Klasse A und dem Zuweisen von or aus _no in Klasse B anhand des Python-3.9-Beispiels und des Verhaltens von mypy 0.931 zu reproduzieren. Verfolge anschließend den Type-Checking-Pfad, der die Signaturen von ior und or vergleicht, und überprüfe dann das Ergebnis für die anderen im Bericht aufgeführten arithmetischen Dunder-Methoden. Als erledigt gilt die Aufgabe, wenn kompatible zugewiesene Methoden nicht mehr fälschlicherweise den Inkompatibilitätsfehler auslösen, während tatsächlich inkompatible Signaturen dies weiterhin tun.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

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