Python 3.7: dataclass does not raise `TypeError` for `eq=False`
When you don't define __eq__
, __eq__
will resolve to object.__eq__
. That is what's happening when you create a dataclass with eq=False
.
object.__eq__(self, other)
is False unless self is other
, i.e. unless the two are the same object.
In python3.7, given the following dataclass definition
@dataclass(eq=False)
class Number:
val: int
the expected result for Number(1) == Number(1)
is False
. This is correct since setting eq = True
only overrides the default python-object equality function, which just checks for identical references (same as Number(1) is Number(1)
, which might more obviously evaluate to false
) in this case.
The dataclass specification is a bit lacking here. It explains the eq
parameter with
eq: If true (the default), an __eq__ method will be generated. This method compares the class as if it were a tuple of its fields, in order. [...]
but in order to understand the issue you ran into, you also need to know that the basic python object already comes with an __eq__
function:
>>> class A: pass
...
>>> dir(A())
['__class__', '__delattr__', ... '__eq__', ...] # has __eq__ already