Repository navigation
Conversation
ParameterScale.__init__ now sets each bracket's parent to the scale, and homogenization links any ParameterNode it builds to replace a placeholder Parameter (through the new ParameterNode.replace_child), so a loaded tree behaves like its clone when Parameter.update clears cached at-instant values and marks ancestors as modified. The country template reform links the bracket it appends. A child whose name matches one of the node's own attributes or methods (for example parent, children or modified) is no longer set as an attribute, so it can no longer replace the node's own state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
replace_child now detaches the previous child only while it still belongs to the node, so a child moved under the replacement keeps its link. The check for names that must not be set as child attributes now looks only in the node's classes, not their metaclasses, so children named register or mro stay reachable as attributes, and the blocked names are computed once per class. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Parameter or ParameterScale stored in children directly has no parent attribute, so replace_child raised after linking the replacement and before clearing cached values. It now reads the attribute with a default. Tests for reserved child names now check a root node and its clone, where an overwrite stays visible, and the add_child docstring says which names are not set as attributes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
replace_child now rejects a non-string name, the node itself or one of its ancestors before changing anything, so a rejected call leaves the tree as it was instead of a cycle or a partial update. It no longer detaches the previous child. Detaching needed a scan of the holder's children to tell whether the child was still held under another name, which made homogenization quadratic for holders with many replaced children. The previous child now keeps its link, as homogenization's replaced placeholders did before. Corrects the comment in the reserved-name test: __init__ resets modified on a root, so only the clone shows an overwrite of that attribute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Homogenization is its only caller, so the fix adds no public API. Removes the changelog fragment that announced it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
added a commit
that referenced
this pull request
Nov 17, 2026
Fixes from the first review round: - A deep copy or pickle of a node at instant resolves every child first, so it is a snapshot that holds no parameter nodes, as when every child was built up front. Before, a partly read node carried its unread parameter nodes, and through their parents the whole tree: 128 MB and 22 s to deep-copy a policyengine-us CTC node, against about nothing on master. A copied cached node keeps the values it was copied with, as on master. - The lazy state is copied as it is once complete, so a copy's _children and its state stay one mapping and add_child on the copy is listed. - add_child on a fully read node no longer changes what attribute reads return on its other copies (the resolved children and the items were one dictionary). Items are read from the complete mapping, so direct writes to _children are items, as before. - A parameter node's at-instant cache is an _AtInstantCache, a dict that copies and pickles from a one-step copy of its items. Copies stay safe while other threads read, and parameter nodes sharing one cache still share its copy. ParameterNode's own __getstate__ and __copy__ are gone. - ParameterScale sets each loaded bracket's parent, as clone already did and as #660 does. Parameter.update and add_child below a bracket now clear the caches above the scale, so a reform of a bracket reads the same whatever was calculated before it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
Parameter.updatewalks upward throughparentto clear cached at-instant nodes and to mark ancestors as modified. Two places built parameter trees without those links, so a tree loaded from files behaved differently from itsclone(), which links every node:ParameterScale.__init__never setbracket.parent. It now sets it to the scale.homogenize_parameter_nodestored theParameterNodeit builds for a further breakdown level inchildrenwithout linking it, and the holder's attribute kept pointing at the placeholderParameter. It now uses a new private method,ParameterNode._replace_child, which:The
hasattr(node, "children")check is nowisinstance(node, ParameterNode).The country template reform that appends a bracket now links it to its scale.
add_child,_replace_childandcloneno longer set a child as an attribute when its name matches one ofParameterNode's own instance attributes (parent,children,name,modified, …) or a method or class attribute of the node's classes (clone,merge, …). Metaclass attributes such asregisterormroare not blocked. A blocked child stays reachable throughchildren[...]and at-instant lookups, soparameters(period).x.parentis unchanged.Before this change:
childrenbroke loading when another child followed it, and brokeclone()otherwise;parentreplaced the root's parent link;parentadded by homogenization replaced the holder's parent link;clone()of a node with a child namedmodifiedsetmodifiedto that child, which is truthy.Effect on the policyengine-us 2.144.2 tree
parent = None(loaded tree)childrenparent, 1 namedmodified; both names are blocked on purpose)gov.irs.credits.ctc.amount.base[0].amount, loaded tree / clonegov.states.mt.dphhs.liheap.payment, reported modified because its child namedmodifiedhad replaced the flag.Simulation.check_macro_cache(the only reader ofmodified) now sees bracket updates under whole-node dependencies that contain scales, such asgov.usda.snapandgov.ssa, whichsnapandsnap_if_takes_updeclare.gov.irs.credits.ctc.amount.base[0].amountfollowing a first simulation, the next simulation still gave the oldctc_maximum(4,400). With this branch it gives the new one (6,400).On the policyengine-us tree, the only names this change no longer sets as attributes are
parent(Medicaid eligibility categories) andmodified(Montana LIHEAP payment), all from YAML files. For directory-loaded trees, 3.36.1 already gave those attributes the holding node and theFalseflag, because the holder'sadd_childand__init__re-assigned them afterwards. So attribute reads of those names on a loaded tree are unchanged. A scan of the parameter folder of a policyengine-uk checkout found no blocked names.Build time
CountryTaxBenefitSystem()did not get measurably slower. These alternating runs were made before the last commit:_replace_childcheaper and private. I could not repeat the wall-clock runs on a quiet machine after it._replace_child(19,622 calls per build, including the cache clearing) takes 0.43 s cumulative, and the blocked-name check 0.49 s, in a profiled build that took 228 s on a heavily loaded machine.Changes callers will see
Simulation.check_macro_cachetherefore stops reusing cached results for variables that declare those nodes as dependencies. This is the intended fix.ParameterNodeinstead of the placeholderParameter. For example,amount.B("2021-01-01")returns a node at that instant rather than0, andamount.B.update(...)raises. This affects 9,811 US attributes, and at-instant lookups are unchanged.clone()now keeps the original scale asparent, as cloning any other non-root node already did. Updating a standalone cloned bracket therefore clears the original tree's caches and marks it modified.policyengine_us/tests/test_pinned_tbs_sharing.py::test_an_edit_to_a_loaded_tree_reaches_the_next_simulationassertsloaded.parameters.gov.irs.credits.eitc.max.brackets[1].parent is None, the unlinked state of released core. It will fail on that precondition with this change. That line, and the comments in that file that describe unlinked brackets, need updating when policyengine-us moves to a core release containing this change.utils/parameter_labels.pywalksparentto find the ancestor with breakdown metadata, so it now generates labels for homogenized US parameters. Running that module from a .py checkout against both versions: of all 128,462 US parameters, those without a label fall from 36,806 to 2,606. No existing label changes, and there are no errors.scale.bracketsmust setbracket.parent = scaleitself, as the template reform now does.copy.deepcopyof a single bracket now copies the whole tree throughparent, as it already did for every other node type. I found no code in core, policyengine-us, policyengine-uk or .py that deep-copies a single bracket.Limits
ParameterNode's own instance attributes are protected, not instance attributes that a subclass sets itself. No subclass in core or policyengine-us sets one.Reform.modify_parametersdeep-copies at-instant caches, and the template reform edits values in place without clearing them. That is unchanged here.Interaction with #570
#570 (resolve parameter nodes at instant on first read) changes
parameter_node_at_instant.py, its tests, a test fixture and a changelog fragment. This PR shares no files with it.childrenon first access. They depend onParameterNode._at_instant_cachebeing cleared upward when a parameter changes, and this change makes that clearing reach every ancestor of brackets and homogenized nodes.ParameterNodeAtInstant.add_child(onmaster, and changed by Resolve parameter nodes at instant on first read #570) still sets each child as an attribute without the blocked-name check. This PR leaves at-instant nodes unchanged.Tests
tests/core/parameters/test_parameter_tree_parent_links.pyandtests/core/parameters/operations/test_homogenization_parent_links.py, 28 tests in all.master's.ruff format --checkandruff check. I did not run the full suite.python -Pfrom outside the checkout, so the "before" environment imports the released core._replace_childleft cached values stale._replace_childdetached a previous child it should not have, or failed partway. They are fixed by no longer detaching._replace_childaccepted the node itself, an ancestor or a non-string name and left a changed tree behind._replace_childwas public during review. It is now private because homogenization is its only caller, so this PR adds no public API.🤖 Generated with Claude Code