python / python/mypy

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

Aperta
#14,956 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in mypy-play e confronta il modo in cui il checker valuta i metodi sovraccaricati add e iadd. Traccia il controllo di compatibilità per queste signature dunder delle operazioni binarie e stabilisci il comportamento previsto per la diagnostica o per il tipaggio accettato. Il lavoro è completato quando il caso segnalato ha un risultato chiaro e motivato e un test di regressione lo copre.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.