python / python/mypy

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

Aperta
#11,941 1 commento 1 reazione 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

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

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 riproducendo la differenza segnalata tra la definizione di or nella classe A e l’assegnazione di or da _no nella classe B, usando l’esempio Python 3.9 e il comportamento di mypy 0.931. Traccia il percorso del controllo dei tipi che confronta le firme di ior e or, quindi verifica il risultato con gli altri metodi dunder aritmetici elencati nel report. Il lavoro è completato quando i metodi assegnati compatibili non producono più erroneamente l’errore di incompatibilità, mentre le firme realmente incompatibili continuano a produrlo.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.