New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataclass __foo__ methods inheritance is not working #81666
Comments
Hi, When using inheritance with dataclass, "standard" instance methods that are provided with a default behavior thanks to dataclass are not overridable using inheritance. Please see the sample below (or the attached file): import dataclasses
@dataclasses.dataclass
class A:
def __eq__(self, other):
return True
@dataclasses.dataclass
class B(A):
pass print(A() == 1) # Returns True as expected Thanks again |
https://docs.python.org/3/library/dataclasses.html#inheritance
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 print(A() == 1) # Returns True as expected Output ./python.exe ../backups/bpo37485.py [0] Line 922 in 7cb9204
|
I agree with xtreak that this works as designed and isn't a bug. |
I personally find it easier to understand if inheritance is the default behaviour, instead of having to explicitly disable a feature that's seems primarily designed for non-herited dataclasses. But whatever suits best the community :-) Thanks for the quick reply |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: