-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
object.__eq__ and other comparisons should have return type Any #3685
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
Please see the thread linked above for details. The statement about raising |
I would be happy to open a PR … |
I am honestly not convinced. I would argue that the default assumption is that |
@jonasrauber I very much agree that something needs to be done about Related discussions: |
This is a case where |
|
OK, but I think you should reconsider closing this instead of fixing it. The non-standard behavior is the annotation in typeshed. It's non-standard because it disagrees with the runtime semantics via implementation of CPython (the only real standard we have for Python). This program has a type error: def f(x: object) -> int:
return x == 0 But a static type checker that uses the typing in typeshed will not catch it without special handling of the type annotations in class object. I would say instead that the annotations in typeshed should be correct and then that checkers could implement something else if they wanted to. |
No it doesn't. |
Are we both talking about the runtime behavior? |
I suspect the point was that def f(x: object) -> int:
return x == 0 has no type errors assuming that the type of class A:
def __eq__(self, other) -> str:
return "YES"
f(A())
|
Rich comparisons have been in Python since 2.1. There's nothing non-standard about it, as @kmillikin said. I have some snippets here to speed up debugging (I do that with
I also don't understand how this is supposed to work with Numpy. Perhaps we need something like |
I'm here because SQLAlchemy also has Actually, I believe that Brython uses <= a little interestingly too: My first reaction to Brython's was sadness, but in the larger context of SQLAlchemy, Numpy and Brython, I'm getting over it. :) Here's my test program:
So apparently this has worked since 2.1.0. |
I have encountered the same problem. I am making a query builder for specific needs and it is strange that Additionally, I want to quote pep-0207
|
One thing that really stands out about Python compared to other languages, is that it is a great host for embedded domain specific languages (DSLs). This bug means that type checkers that use typeshed for typing Put another way: this typing is (gratuitously) unsound with respect to the runtime semantics. |
As mentioned before, type checkers will happily accept those comparisons if the LSP violation in the overloading classes is class Foo:
def __eq__(self, other: object) -> str: # type: ignore[override]
return "Hey!"
reveal_type(Foo() == Foo()) # revealed type is `str` While overloading the operators in common, it is an LSP violation and unsound. Using |
@srittau Can you please give us a reference to prove it? According to PEP207 it is not required to return bool for any comparison operator. I do not see there any difference between |
|
Given the implementation, I would suggest the return type should be
Union[NoReturn, Any]
.object.__eq__
always throws aNotImplementedError
(hence theNoReturn
) andAny
allows sub-classes to override the return type to anything they like.Originally posted by @Victor-Savu in python/mypy#2783 (comment)
Please fix the return type of
__eq__
and others in typeshed as described in the comment above.__eq__
not returning a boolean is very common, most notably NumPy arrays performing elementwise comparisons.The text was updated successfully, but these errors were encountered: