python / python/mypy

Signatures of *in-place binary operation* and *binary operation* are incompatible

Offen
#14,956 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
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beschreibung

Bug Report

If I define custom binary operations (both regular & in-place versions) using __dunder__ methods like __add__ & __iadd__, I'm having odd

Signatures of "__iadd__" and "__add__" are incompatible

and I don't understand why, what is a "compatibility" the error is telling about?

To Reproduce

Example
from __future__ import annotations

from typing import Any, overload


class MyClass:
    @overload
    def __add__(self, other: MyClass) -> MyClass:
        ...

    @overload
    def __add__(self, other: Any) -> Any:
        ...

    def __add__(self, other: Any) -> Any:
        return (self
                if isinstance(other, MyClass)
                else NotImplemented)

    @overload
    def __iadd__(self, other: MyClass) -> MyClass:
        ...

    @overload
    def __iadd__(self, other: Any) -> Any:
        ...

    def __iadd__(self, other: Any) -> Any:
        if not isinstance(other, MyClass):
            return NotImplemented
        return self

https://mypy-play.net/?mypy=latest&python=3.8&gist=819e2627d0a17df446a5d7b2b3631f11

Expected Behavior

Ideally as a user of static type analyzer I want to get an error when I misuse a type hint for parameters/return type and get details about what's wrong with my code according to some rules, current message is too vague and the error isn't going away even when I put typing.Any everywhere in one of the methods annotations (only when I remove all typing.overloads). This is also the case when I use typing.Self instead of MyClass

Actual Behavior

error: Signatures of "__iadd__" and "__add__" are incompatible  [misc]

Your Environment

  • Mypy version used: mypy 1.1.1 (compiled: yes)
  • Mypy command-line flags: both none & --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python3.8

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 vergleiche, wie der Checker die überladenen Methoden add und iadd auswertet. Verfolge die Kompatibilitätsprüfung für diese Dunder-Signaturen von Binäroperationen und stelle das beabsichtigte Diagnose- oder akzeptierte Typisierungsverhalten fest. Als abgeschlossen gilt die Aufgabe, wenn der gemeldete Fall ein klares, begründetes Ergebnis hat und ein Regressionstest ihn abdeckt.

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
35/100

Neue Issues direkt in Ihr Postfach

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