MyPy checks fail when overriding __eq__ to return a custom type and calling it as a right-operator
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
When I override __eq__ and return a custom type, the MyPy checks succeed when my custom type is on the LHS of the equality, but fail when my custom type is on the RHS of the equality.
Here is an example to illustrate: line 14 yields no error but line 16 yields an error:
Incompatible types in assignment (expression has type "bool", variable has type "MyClass")
from typing import Any
class MyClass:
def __eq__(self, other: Any) -> 'MyClass':
return self
def test_tmp() -> None:
x = MyClass()
y = 1.
result: MyClass
result = (x == y)
assert type(result) == MyClass
result = (y == x)
assert type(result) == MyClass
The test passes, i.e. I get back an object of type MyClass when x is on the LHS or RHS of my equality.
NB - there is also a MyPy error on line 6, Return type of "__eq__" incompatible with supertype "object" - this is because of https://github.com/python/mypy/issues/2783. I think my issue is separate from this one.
Version numbers: Python 3.6.5, mypy 0.641.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo lo snippet Python fornito con mypy e confronta i tipi inferiti per x == y e y == x. Analizza la gestione dell’operatore di uguaglianza che produce bool nel caso del lato destro; il lavoro è completo quando l’assegnazione a result: MyClass viene accettata in modo coerente, mentre l’errore separato di supertipo di __eq__ rimane distinto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100