Skip to content

Better error message when __eq__ has unexpected signature #2055

Closed
@JukkaL

Description

@JukkaL

It's pretty common to have __eq__ with a non-object argument type, and mypy will just complain that it's not compatible with object (as object defines __eq__ with an object argument). It would be better to display a message that shows how to properly implement __eq__. Here's an idea (probably needs polish):

x.py:23: error: Argument 1 of "__eq__" incompatible with supertype "object"
x.py:23: note: It's recommended for "__eq__" to work with arbitrary objects.
x.py:23: note: This example shows how you can implement "__eq__":
x.py:23: note:
x.py:23: note: class Foo(...):
x.py:23: note:     ...
x.py:23: note:     def __eq__(self, other: object) -> bool:
x.py:23: note:         if not isinstance(other, Foo):
x.py:23: note:             return NotImplemented
x.py:23: note:        return <logic to compare equality of two Foo instances>

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions