# Method "\_\_eq\_\_" overrides class "object" in an incompatible manner - well, not always

**URL:** <https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878>\
**Category:** Python Help\
**Tags:** typing\
**Created:** [January 10, 2024, 11:30am UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878 "2024-01-10T11:30:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptsl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ptsl/32/18617_2.png) [@ptsl](https://discuss.python.org/u/ptsl)\
**Post date:** [January 10, 2024, 11:30am UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878/1 "2024-01-10T11:30:08Z")

</div>

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__ `.

 ![eq-annotations](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/d/c/dc0b7d011d545174f71ef201e32df6c6f3588bda.png)

The error message generated by Pyright is as follows:

```auto
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__ `?"

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [January 10, 2024, 11:34am UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878/2 "2024-01-10T11:34:51Z")

</div>

` __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`.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 10, 2024, 12:02pm UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878/3 "2024-01-10T12:02:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ptsl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ptsl/32/18617_2.png) [@ptsl](https://discuss.python.org/u/ptsl)\
**Post date:** [January 10, 2024, 12:48pm UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878/4 "2024-01-10T12:48:34Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![ptsl](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ptsl/32/18617_2.png) [@ptsl](https://discuss.python.org/u/ptsl)\
**Post date:** [January 10, 2024, 12:49pm UTC](https://discuss.python.org/t/method-eq-overrides-class-object-in-an-incompatible-manner-well-not-always/42878/5 "2024-01-10T12:49:35Z")

</div>

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