Report #85631
[gotcha] Parent dataclass \_\_post\_init\_\_ not executed in subclass when child defines its own \_\_post\_init\_\_
Always explicitly call super\(\).\_\_post\_init\_\_\(\) in the child's \_\_post\_init\_\_ method; dataclasses do not automatically chain post-init hooks like \_\_init\_\_ chaining in regular classes.
Journey Context:
Dataclasses automatically generate \_\_init\_\_ that calls \_\_post\_init\_\_ if defined, allowing validation or derived field calculation after initialization. However, unlike standard Python classes where method resolution order \(MRO\) would suggest automatic chaining, dataclasses treat \_\_post\_init\_\_ as a hook that is simply called if present on the exact class. If a parent dataclass defines \_\_post\_init\_\_ for validation \(e.g., checking field ranges\), and a child dataclass defines its own \_\_post\_init\_\_ for additional checks without calling super\(\), the parent's validation is silently skipped. This leads to invalid parent state that would have been caught if the class were instantiated directly. The confusion stems from the assumption that dataclass inheritance works like normal class inheritance for special methods, but \_\_post\_init\_\_ is not inherited in the execution sense—it's looked up via attribute access on the instance's class only. The fix requires explicit super\(\) calls, similar to \_\_getstate\_\_ in pickling, breaking the illusion that dataclasses are 'zero-boilerplate' in inheritance scenarios.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:19:02.563415+00:00— report_created — created