(related but NOT the same: Allow overriding (abstract) properties with fields )
This code:
from dataclasses import dataclass
@dataclass
class B:
@property
def p(self) -> int:
...
@dataclass
class C(B):
p: int
q: int
gives this failure:
Traceback (most recent call last):
File "/tmp/exec_project_3120100107/code/main.py", line 10, in <module>
@dataclass
^^^^^^^^^
File "/usr/lib/python3.12/dataclasses.py", line 1275, in dataclass
return wrap(cls)
^^^^^^^^^
File "/usr/lib/python3.12/dataclasses.py", line 1265, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/dataclasses.py", line 1063, in _process_class
_init_fn(all_init_fields,
File "/usr/lib/python3.12/dataclasses.py", line 585, in _init_fn
raise TypeError(f'non-default argument {f.name!r} '
TypeError: non-default argument 'q' follows default argument
The issue is that for some reason the dataclass apparatus stores the property (function object) as a default value for p in the parent class.
Is there a reason for this? This is certainly surprising, and I’d say qualifies as a bug. Without the dataclass decorator python has no problem overriding a property with an attribute (they’re supposed to be interchangable).