-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Return type of "__eq__" incompatible with supertype "object" #2783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is because of the return type of |
Thank you for a quick response @ilevkivskyi. How would you proceed in case like this? I thought about
which is a bit suboptimal but seems acceptable. Does it make sense to you? |
class Expr:
def __eq__(self, other: Any) -> 'Expr': # type: ignore
return Expr()
def __ne__(self, other: Any) -> 'Expr': # type: ignore
return 42 # Error: Incompatible return value type (got "int", expected "Expr")
x = Expr() == 0
x.bad # Error: "Expr" has no attribute "bad" while with EDIT: extended the example. |
I wonder if the return type of |
Thanks @ilevkivskyi |
@ilevkivskyi Using your suggestion of appending |
I got this is issue by looking for: Argument 1 of "eq" incompatible with supertype "object" from typing import Union
class Expr:
def __eq__(self, other):
# type: (Union[Expr, str]) -> bool
return False In this case, I tried to put the '# type: ignore' in the same line as the comment type annotation without luck. |
It needs to be on the line with the function declaration |
Given the implementation, I would suggest the return type should be |
That said, I agree with you that we need to broaden the use cases that are supported. I like the unsafe override decorator proposal in #5704, and I think that that along with addressing some usability issues would go a long way. |
Some related discussion in #6710, which is more about the unexpected downstream effects of the |
I am so sorry, you are right! It does not throw NotImplementedError, instead it returns NotImplemented: assert object().__eq__(object()) == NotImplemented Which would be fine, because it is consistent with
Thanks a lot for linking to this! It is a difficult problem and I am happy to see you and the maintainers of mypy give it well-deserved attention. |
Now it's possible to use |
Seems that __eq__ should not raise NotImplementederror python/mypy#2783 (comment)
Seems that __eq__ should not raise NotImplementederror python/mypy#2783 (comment)
Let's say I have a simple class intended for symbolical computations where
==
and!=
evaluate to another expression:This will result in following errors:
As far as I understand Python data model doesn't require rich comparison methods to return
bool
. Is it Mypy specific requirement or is there something wrong with my class definition?The text was updated successfully, but these errors were encountered: