python / python/mypy

__r<magic>__ methods for reflected (swapped) operands doesnt duck type int to float

Aperta
#9,388 2 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

It seems like ints are not compatible with floats when used in the __r<magic>__ methods for reflected (swapped) operands like __radd__ the same was as in normal operands if the int/float is used as a type parameter in a generic type.

As a workaround it works if I specify a union type with the int case as well, but would be nice to not have to do that since its not needed otherwise.

To Reproduce

code.py:

from typing import List

class Vec:
    def __add__(self, other: List[float]) -> None:
        pass

    def __radd__(self, other: List[float]) -> None:
        pass

r1 = Vec() + [1]
r2 = [1] + Vec()  # error line
r3 = [1.0] + Vec()

Type checking the above code with python -m mypy code.py

Expected Behavior

I expect the r2 case to work as well

Actual Behavior

I get this error message for the r2 line: error: Unsupported operand types for + ("List[int]" and "Vec")

Your Environment

  • Mypy version used: 0.782
  • Mypy configuration options from mypy.ini (and other config files): No special config
  • Python version used: CPython 3.8.4, 32 bit.
  • Operating system and version: Windows 10

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 dell'issue in code.py ed esegui python -m mypy code.py usando i casi segnalati. Traccia il modo in cui il controllo degli operandi gestisce __add__ rispetto a __radd__ riflesso quando List[int] viene confrontato con List[float]; il lavoro è completo quando il caso r2 supera il type-checking senza richiedere un workaround con una union, mentre i casi esistenti rimangono corretti.

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
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.