Message413555
The fact that it's never been needed in the years that dataclasses and attrs have existed tell me it's kind of a niche requirement.
This does not seem like the ugliest code I've ever seen:
if hasattr(super(), "__post_init__"):
super().__post_init__()
or the probably more efficient, but more lines of code:
try:
post_init = super().__post_init__
except AttributeError:
pass
else:
post_init()
As always with calling super functions, the whole hierarchy needs to cooperate. Say you had a dataclass:
@dataclass
class Base:
def __post_init__(self, some_arg):
pass
How would an arbitrary derived class know how to call this? It can't. There has to be knowledge of the base class's requirements already. Surely knowing "__post_init__ must be called with some_arg" isn't too different from "I know __post_init__ doesn't exist". I don't think adding ways to make the "always call super" pattern easier is a good idea.
I'm still unconvinced, but I'll hold off on making a decision to see if there's more support. Maybe taking it to python-ideas would be worthwhile. |
|
Date |
User |
Action |
Args |
2022-02-19 17:44:03 | eric.smith | set | recipients:
+ eric.smith, NeilGirdhar, veky, sobolevn |
2022-02-19 17:44:03 | eric.smith | set | messageid: <1645292643.34.0.850638654611.issue46757@roundup.psfhosted.org> |
2022-02-19 17:44:03 | eric.smith | link | issue46757 messages |
2022-02-19 17:44:03 | eric.smith | create | |
|