Method "__eq__" overrides class "object" in an incompatible manner - well, not always

Hello,

I am in the process of adding type annotations to some legacy code.

Below is a minimal example illustrating my issue. I apologize for providing the source code as a screenshot, but I wanted to highlight that Pyright, in the strict mode, underlines the methods __eq__ and __ne__, but not __lt__ and __gt__.

The error message generated by Pyright is as follows:

Method "__eq__" overrides class "object" in an incompatible manner
  Parameter 2 type mismatch: base parameter is type "object", override parameter is type "File"
    "object" is incompatible with "File"

I have researched this issue and found advice suggesting the use of other: object and checking the actual type with isinstance.

But I feel there is something missing. Could someone provide more insight into the rationale behind the varied typeshed implementations of __eq__/__ne__ and __lt__/__gt__?"

__eq__ should work with arbitrary second arguments, i.e. File() == object() should return False instead of raising an exception. To do this, add an isinstance check and return NotImplemented if the type of other isn’t File.

1 Like

Your __ne__ method has the same implementation as the __eq__ method.

Thank you!

Oh, yes. The actual implementation is much longer. I made a mistake when I copy/pasted lines to this minimal example.