Skip to content
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

MyPy checks fail when overriding __eq__ to return a custom type and calling it as a right-operator #5951

Open
migwellian opened this issue Nov 26, 2018 · 0 comments
Labels
bug mypy got something wrong

Comments

@migwellian
Copy link

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 #2783. I think my issue is separate from this one.

Version numbers: Python 3.6.5, mypy 0.641.

@AlexWaygood AlexWaygood added the bug mypy got something wrong label Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants