Karthikeyan Singaravelan <[email protected]> added the comment:
https://docs.python.org/3/library/dataclasses.html#inheritance
> When the dataclass is being created by the dataclass() decorator, it looks
> through all of the class’s base classes in reverse MRO (that is, starting at
> object) and, for each dataclass that it finds, adds the fields from that base
> class to an ordered mapping of fields. After all of the base class fields are
> added, it adds its own fields to the ordered mapping. All of the generated
> methods will use this combined, calculated ordered mapping of fields. Because
> the fields are in insertion order, derived classes override base classes.
I think here in the example __eq__ is defined in A and is inherited by B but
since eq=False is not explicitly defined in the decorator the dataclass will
use it's own definition of eq and override it at [0] . So using eq=False will
work as expected. I propose closing this as not a bug.
import dataclasses
@dataclasses.dataclass
class A:
def __eq__(self, other):
return True
@dataclasses.dataclass(eq=False) # By default eq=True
class B(A):
pass
print(A() == 1) # Returns True as expected
print(B() == 1) # Returns True since inherited A.__eq__ is not overriden with
eq=False
Output
./python.exe ../backups/bpo37485.py
True
True
[0]
https://github.com/python/cpython/blob/7cb9204ee1cf204f6f507d99a60f7c5bb359eebb/Lib/dataclasses.py#L922
----------
nosy: +xtreak
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37485>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com