Repository navigation
Conversation
Tests that fail before the change: Population.current_law reads a variable declared with current_law_read, an undeclared read raises, each declared call is reported to the tracer, a reform simulation builds no current-law copy while it is constructed, the copy is built from the inputs held when it is first needed and made read-only by the first declared call, and assigning baseline replaces the copy. Two existing tests read the baseline branch before anything built it; they now build it through simulation.baseline, or drop it only if it exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Core requires a label on every variable, and income tax is calculated in float32, so the tests compare it with the expected amounts to one part in a million. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A variable that must be calculated after another raises in the current-law copy when the copy has not requested that other variable, as it did when construction built the copy, even if the reform simulation requested it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Formulas read current law through declared calls, each reported to the
simulation's tracer (record_current_law_call; the full tracer keeps them on
TraceNode.current_law_calls):
- Population.current_law(variable, period) reads a variable declaring
current_law_read = True, and raises UndeclaredCurrentLawReadError for any
other; without a reform it returns the simulation's own value.
- Simulation.compares_with_current_law and Simulation.current_law_system,
which never build a copy.
- Simulation.current_law_simulation(), the simulation that calculates
current-law values.
A reform simulation no longer branches its current-law copy while it is
constructed. The overridable _build_current_law_simulation() builds it the
first time it is needed: a branch named baseline under the default system
instance, keeping the inputs held then and dropping calculated values. The
first declared call that reaches it makes it read-only. Branches read their
parent's copy, and clone() and subsample() keep a copy only if one was built.
Simulation.baseline is now a deprecated property: reading it returns the copy,
left writable, and is reported to the tracer; assigning a simulation makes it
the copy, kept as given, and assigning None stops the comparison.
get_branch("baseline") still returns the copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: current_law is projected through entity
projections like a population call; current_law without a period raises
before building the copy and names the calling code; a derivative does not
depend on whether the copy was built before it; removing the registered copy
lets get_branch("baseline") make a new branch; a simulation saved with
baseline in its instance dictionary keeps it; an AttributeError while building
the copy is not read as "no baseline".
Tests for behaviour that already holds: options pass through current_law; a
copy built inside a calculation ignores that calculation's reform values; a
clone of a simulation with a read-only copy gets its own copy; a branch of a
branch reads the first simulation's copy; a deep copy keeps its own copy; the
declaration check reads an assigned simulation's system. One edited test now
builds the baseline by reading it, and two test modules no longer say
construction builds the branch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- current_law is projectable, so person.household.current_law(...) returns
one value per person, as person.household(...) does.
- current_law checks the period before building the copy, so a missing
period builds nothing and the error names the calling code.
- derivative builds the current-law copy before cloning, so its result does
not depend on whether an earlier call built the copy: the changed clone
compares with the current law of the simulation's own inputs, as before.
- get_branch("baseline") returns a built copy missing from branches only on a
read-only simulation; elsewhere, code that removed the registered copy gets
a new branch, as before.
- subsample builds a copy core built again with _build_current_law_simulation,
so an override applies and the copy starts with nothing requested; other
baseline branches are rebuilt as before.
- The builder also clears pending cache invalidations.
- A simulation that kept baseline in its instance dictionary (saved by an
earlier core) keeps it as its current-law copy.
- An AttributeError while building the copy through the baseline property is
raised as a RuntimeError, so getattr(simulation, "baseline", None) does not
read it as "no baseline".
- Docstrings no longer say construction builds the copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: a read-only simulation registers the
current-law copy it builds, so a clone of it keeps its copy registered and
get_branch("baseline") and subsample see it; a saved simulation's own
baseline branch, kept in its instance dictionary by an earlier core, is
copied by clone rather than shared; assigning baseline on a read-only
simulation raises.
Tests for behaviour that already holds: subsample builds a read-only copy
again as a writable one, and does so with an overridden builder; a copy built
in a calculation that runs again after an input change reads the new input.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A read-only simulation registers the current-law copy it builds, so a
clone of it keeps its copy registered and get_branch("baseline") and
subsample see it. get_branch("baseline") now returns the record's copy
only while it is not built.
- clone() and get_branch() turn a baseline kept in the instance dictionary by
an earlier core into a record before deciding, so a clone copies a saved
simulation's own baseline branch rather than sharing it.
- Assigning baseline on a read-only simulation raises
ReadOnlySimulationError, as assigning tax_benefit_system does.
- The builder and subsample set up the baseline branch with one helper,
_make_current_law_branch, and subsample gives the simulation a new record
in both of its paths.
- Docstrings: the builder is also called by subsample; get_branch's
"baseline" case applies to a reform simulation, not its branches; a
current_law result from a read-only copy is non-writable.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests that fail before the fixes: a clone announced as a branch outside get_branch (as policyengine-uk's subsample makes one, to share arrays) builds its own current-law copy from its own inputs and does not keep its source alive through the current-law record; assigning baseline on a simulation saved by an earlier core drops the saved attribute, so its clones do not carry it. Test for behaviour that already holds: a traced clone of a simulation whose copy is built traces the clone's copy in the clone. The clone test module's docstring now says when a test's clone builds a copy of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The template's module-level system names the last simulation built from it, which keeps that simulation alive whatever the clone holds, so the test now checks that the clone's current-law record refers to the clone and not to its source. It fails before the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A clone announced as a branch outside get_branch (policyengine-uk's subsample announces one to share arrays) gets a current-law record of its own that does not refer to its source, so it no longer keeps the source alive, and builds its own copy from its own inputs. get_branch gives the branch its parent's record once the clone is made. - Assigning baseline drops a baseline kept in the instance dictionary by an earlier core, so clones and pickles no longer carry it. - Docstrings: current_law_system names the default system of the simulation the copy is built from; current_law_simulation states that the copy is registered, also on a read-only simulation; current_law states the exception for an assigned simulation; TraceNode.current_law_calls says the values read are children only when the calculating simulation shares the tracer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 2, 2026
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.
Fixes #674
What this adds
Formulas read current law through declared calls, and the simulation that calculates current-law values is built only when something needs it.
Declared calls.
Population.current_law(variable, period, options)(also through projections and on group populations) returns the variable's value under current law, for a variable that declares the newVariableattributecurrent_law_read = Truein the current-law tax-benefit system. Any other variable raises the newUndeclaredCurrentLawReadError. Without a reform it returns the simulation's own value.Simulation.compares_with_current_lawandSimulation.current_law_system(properties) never build a copy.Simulation.current_law_simulation()returns the current-law copy, or the simulation itself without a reform.Tracer. Each declared call is reported to the simulation's tracer through the new
record_current_law_call(call, variable, period, branch_name).SimpleTracerignores it,FullTracerkeepsCurrentLawCallrecords in the newTraceNode.current_law_calls, and core skips a tracer that lacks the method. Reads ofsimulation.baselineare reported too, as callbaseline. The value acurrent_lawcall reads stays an ordinary child calculation, so a run's dependency record shows which formulas read current law or asked whether the run differs from it.The copy, built on demand.
Simulation.__init__no longer callsget_branch("baseline"). The overridable_build_current_law_simulation()builds the copy the first time something needs it:baseline, registered inbranches(also on a read-only simulation, unlike its other branches) and traced in the simulation;default_tax_benefit_system_instance, with holders rebound and its own list of input variables;The first declared call that reaches the copy makes it read-only with
make_read_only()(Add read-only simulations #672).simulation.baselineis deprecated. It is now a property:Nonewithout a reform;Nonestops the comparison, and assigning on a read-only simulation raisesReadOnlySimulationError.It gives no
DeprecationWarningyet: country packages read and assign it on every simulation they build.Branches, clones and subsamples.
baselinewas its parent's.clone()keeps a copy only if one was built. A clone of a simulation with no copy builds its own later, from its own inputs.subsample()builds a copy that was built again, through the builder.get_branch("baseline")returns the copy, as it did when construction built it.derivative()builds the copy before cloning, so the changed clone compares with the current law of the unchanged inputs, as before.Behaviour changes for existing code
simulation.branches["baseline"]read right after construction raisesKeyError; readsimulation.baseline(or use the declared calls) instead. Two existing tests did this and are adjusted.Compatibility
mainat97cabe17): still gets its own current-law simulation. During construction,migrate_social_security_inputsreadssimulation.baseline, which builds core's copy._rebuild_baseline_branchthen replaces it with policyengine-us's own branch and assigns that, so the copy stays writable and registered.__init__, and assignsbaselineitself. Its subsample announces clones as branches to share arrays; those clones now get a current-law record of their own that does not refer to their source.microsim.baselineright after construction. That works because a copy reached throughbaselinestays writable.del simulation.branches[name], which would raiseKeyErrorinside a read-only copy. Open the branching formulas' temporary branches with Simulation.temporary_branch policyengine-us#11254 changes them tobranches.pop(name, None). No current package lets a declared call reach a copy that core built.Checks
Tests.
tests/core/test_current_law_reads.py, 54 tests. Each test commit precedes the change or fix it tests:001f201d,e1a2d7dd): 29 failed on assertions before the change. The one that passed (get_branch("baseline")returns the copy) also held before. One more test (05485906, the copy starts with nothing requested) failed before its fix. All 31 passed on the change commit8f0a3781.5d591dd6): 6 failed before the fixes in977323b6. The other 6 cover behaviour that already held.be5b67cf): 4 failed before the fixes ind0b42e78.1a90495f,a6bc9250): 3 failed before the fixes in13aad572.Existing tests. Every test in
tests/core/*baseline*,*clone*(simulation and variable clones) and*subsample*, plus the tests elsewhere with "baseline" in their name: 292 tests, each run by node ID, all passing on the final commit. Edited:test_subsample_baseline_branch.py, which now readssimulation.baselinefirst;test_reform_baseline_system.py, which now builds the branch by readingbaseline;test_clone_reform_baseline.py.The full suite was not run locally.
Values before and after (before:
master8eabab83; after: this branch):employment_income,labor_supply_behavioral_response,income_tax,household_net_incomeandmedicaid.derivative()are identical.Review. Three adversarial review rounds by separate review agents, each followed by fixes, each with a test that fails before it:
current_lawthrough projections; a missing period built the copy first;derivative()depended on whether the copy was built; removing the registered copy thenget_branch("baseline")returned it. One medium was not changed: the policyengine-usdelabove.baselinewith its clones.get_branchkept its source alive through the current-law record; assigningbaselineon a saved simulation left the saved attribute.Not run:
make documentation, policyengine-uk, the full test suite.Left as is
del simulation.branches[name]formulas (above): fixed by Open the branching formulas' temporary branches with Simulation.temporary_branch policyengine-us#11254.make_read_only()copies every array the copy still shares with the reform simulation. For a microsimulation that is its input data, once per run. This is Add read-only simulations #672's documented behaviour, and it only happens in runs that make declared calls.migrate_social_security_inputsreadsbaselinebefore_rebuild_baseline_branchruns. An explicit_build_current_law_simulationoverride in policyengine-us would make that independent of the read.Merge order
masteris merged into this branch.changed(a minor release): existing country packages and .py keep working unchanged.Overlapping open PRs
simulation.py,simulation_builder.py) builds a reform simulation's baseline from the situation insideSimulation.__init__. Whichever merges second moves that rebuild into_build_current_law_simulation.simulation.py. Trace parameter reads in yearly formulas and scale reads; add dependency-map #543 is superseded by the merged Record scale, child-list and undated parameter reads in the tracer #664.🤖 Generated with Claude Code