Run post_init for the class being instantiated - #166
Merged
Merged
Conversation
DataClassNode.__init__ called post_init only on the entries of _DATA_CLASS_ANCESTORS, a list that deliberately excludes the class being constructed. A class that used the documented extension point instead of overriding __init__ therefore never had its callback run, and the omission was silent: the callback started firing only once some other class subclassed it. The ancestor list was also in __mro__ order, most derived first, so ancestor callbacks ran in the reverse of constructor order, contradicting the base-up initialization the post_init docstring described. Each class in the hierarchy that defines its own post_init is now recorded at class-definition time, least derived first and ending with the class itself. Selecting on __dict__ keeps an implementation that a subclass inherits without overriding from running once per inheriting class, which preserves the single call that subclasses of a data class already got. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa
# Conflicts: # test/test_dataclasses.py
ESultanik
added a commit
that referenced
this pull request
Sep 9, 2026
The slot accumulation added in the previous commit was written against an earlier `__init_subclass__`, where `ancestors` was in plain `__mro__` order and had to be reversed to merge annotations base-first. PR #166 has since rebuilt that list from `reversed(cls.__mro__)`, so it already arrives base-first and reversing it again inverted the merge order: a diamond `class D(B, C)` produced slots in `B`-then-`C` order rather than the declaration order the ancestors imply. Because none of the edited lines overlapped, the change applied cleanly on top of #166 while its meaning changed underneath it, and the accompanying test failed on the merge commit. Deriving `_SLOTS` from the merged annotations also left `new_slots` written but never read. Ruff does not report it, because `new_slots.append(...)` counts as a reference for F841. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #149
DataClassNode.__init__calledpost_init()only on the entries of_DATA_CLASS_ANCESTORS, which excludes theclass being constructed. A subclass that used the documented extension point instead of overriding
__init__neverhad its callback run, and nothing reported the omission: the callback began firing only once some other class
subclassed it. The ancestor list was also in
__mro__order, most derived first, so ancestor callbacks ran in thereverse of constructor order, contradicting the base-up initialization the
post_init()docstring described.Each class in the hierarchy that defines its own
post_initis now recorded at class-definition time, least derivedfirst and ending with the class itself. Selecting on
__dict__keeps an implementation that a subclass inheritswithout overriding from running once per inheriting class, so subclasses of a data class still get exactly one call.
docs/builders.rstcarried a note documenting the old behavior as a limitation; it is replaced with the inheritedimplementation rule.
No
DataClassNodesubclass in the package definespost_init(graphtage/ast.pyandgraphtage/pydiff.pyuse__init__overrides), so no in-tree node changes behavior.Validation
Three tests in
test/test_dataclasses.pywere confirmed to fail against the unfixed code and pass after the fix:test_post_init_runs_once_per_implementationasserts the call order and count for a three-level hierarchy where themiddle class inherits its
post_init(expected[("Base", "Derived"), ("Derived", "Derived")]; the unfixed codeproduced
[]for the first instantiation).test_post_init_runs_for_direct_subclasscovers theUnquotedNameexample from the docs.test_inheritancewas strengthened: bothFoo.post_initandBar.post_initset distinct flags, so the assertion nolonger passes when only the ancestor's callback runs. It previously passed by accident.
The reproducer from the issue now prints
[('Node1', 'Node1')]for the direct instantiation, and a diamond hierarchycalls each implementation once in base-first order.
Local CI:
ruff check graphtage test docs bindist: passedpytest: 142 passedmake -C docs html SPHINXOPTS="-W --keep-going": build succeededuv lock --check: fails in this sandbox on an unmodified checkout ofmasteras well, because of a globalexclude-newertimestamp. No dependency files were touched.🤖 Generated with Claude Code
https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa