Repository navigation
Conversation
…never kept policyengine-core's read-only simulations (not yet released) give a new branch for a name they have not registered and do not keep it in simulation.branches, so del simulation.branches[name] raises KeyError. The test gives a simulation a get_branch that keeps no new branch and checks the marginal tax rates, the capital gains rate and the employer Social Security and Medicare income tax revenues against plain values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The marginal tax rate helper, the capital gains marginal tax rate and the employer Social Security and Medicare income tax revenue formulas deleted their branch with del simulation.branches[name]. A simulation that keeps no branch it creates, such as one of policyengine-core's planned read-only simulations, raised KeyError there. pop(name, None), which six other formulas already use, removes the branch when the simulation kept it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…name In a branch named as a formula names the branch it creates (mtr_for_adult_1, adult_1_cg_rise, without_employer_ss, without_employer_medicare), get_branch returns the simulation being calculated. The formula then raised that simulation's inputs or neutralized variables in its system. del simulation.branches[name] raised KeyError afterwards; pop(name, None) does not, so the result was wrong. The formulas must refuse before changing anything. state_marginal_tax_rate leaves the value test: it goes through the same statement as marginal_tax_rate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add get_branch_to_change, which raises ValueError when get_branch returns the simulation itself (asked for the simulation's own branch name). The marginal tax rate helper's perturbed branch and the employer Social Security and Medicare revenue formulas use it, so a formula calculated in a branch with its branch name stops before raising that simulation's inputs or neutralizing variables in its system. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11254 +/- ##
============================================
- Coverage 100.00% 82.55% -17.45%
============================================
Files 1 1
Lines 24 86 +62
Branches 0 2 +2
============================================
+ Hits 24 71 +47
- Misses 0 14 +14
- Partials 0 1 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What and why
This prepares for policyengine-core's read-only simulations (
Simulation.make_read_only()). The core change is not released and its PR is not open yet.On a read-only simulation,
get_branchwith a name the simulation has not registered returns a new branch every time and does not keep it insimulation.branches. Four policyengine-us formulas create a branch withget_branch, calculate in it, then remove it withdel simulation.branches[name], which raisesKeyErrorwhen the branch was never kept:marginal_earnings_changeinmarginal_tax_rate_helpers.py, whichmarginal_tax_rate,marginal_tax_rate_including_health_benefitsand the federal, state and FICA component rates use;marginal_tax_rate_on_capital_gainsincapital_gains_responses.py;employer_ss_tax_income_tax_revenue;employer_medicare_tax_income_tax_revenue.Each now removes its branch with
simulation.branches.pop(name, None), as the Missouri TANF, Colorado Medicaid and Medicaid formulas, the behavioral response measurements andtools/tob.pyalready do. On a plain simulationget_branchkeeps every branch it creates, sopopremoves it exactly asdeldid and results are unchanged.A formula calculated in a branch with its own branch name
If a simulation's own branch name is the name one of these formulas gives its branch (
mtr_for_adult_1,adult_1_cg_rise,without_employer_ss,without_employer_medicare, …),get_branchreturns that simulation itself. The formula then raised that simulation's inputs, or neutralized variables in its system, in place. Onmain, thedelraisedKeyErrorafterwards. Withpop, the formula would finish and return a wrong value: for a Texas couple with $80,000 and $30,000 of wages in 2024,marginal_tax_rategave -0.437 for the second earner, where a plain simulation gives 0.2815.The new
get_branch_to_change(tools/branched_simulation.py) raisesValueErrorwhenget_branchreturns the simulation itself, before anything changes. The perturbed branch of the marginal tax rate helper (which the capital gains rate also uses) and both employer revenue formulas create their branch through it. No model formula calculates these variables in a branch with those names, and every such calculation raisedKeyErroronmain.Checked against the core branch
I installed policyengine-core's
read-only-simulationsbranch (eeb3869b) in a scratch environment, with the other dependencies fromuv.lock. The household is one California adult with $50,000 of wages in 2024, plus $20,000 of long-term capital gains for the capital gains rate. The employer revenues are calculated withgov.contrib.crfb.tax_employer_payroll_taxin effect at 100%.mainmarginal_tax_rateKeyError: 'mtr_for_adult_1'state_marginal_tax_rateKeyError: 'state_mtr_for_adult_1'fica_marginal_tax_rateKeyError: 'fica_mtr_for_adult_1'marginal_tax_rate_on_capital_gainsKeyError: 'adult_1_cg_rise'employer_ss_tax_income_tax_revenueKeyError: 'without_employer_ss'employer_medicare_tax_income_tax_revenueKeyError: 'without_employer_medicare'On this branch, each read-only value equals the plain value exactly, and the read-only simulation's
branchesis unchanged afterwards.Test plan
Each new test failed before its fix: the tests are committed before the changes.
policyengine_us/tests/core/test_unkept_temporary_branches.pyruns in the Restcoregroup. Released core keeps every branchget_branchcreates, so the tests replace core'sget_branch(withmonkeypatch) by one that removes each new branch a chosen simulation creates from itsbranchesbefore returning it, as a read-only simulation keeps none. The replacement changes only which branches are kept; the rest of whatmake_read_onlydoes is not reproduced here.test_the_test_simulation_keeps_no_branch_it_createschecks that replacement: a new branch every call, none kept, and the branch itself still keeps its own branches.test_formulas_give_their_plain_values_when_their_branches_are_not_kept, two cases:marginal_tax_rateandmarginal_tax_rate_on_capital_gains;main, both cases raisedKeyError('mtr_for_adult_1','without_employer_ss').test_formulas_refuse_to_calculate_in_a_branch_with_their_branch_name, the same two cases. Each formula raisesValueErrorin a branch with its branch name, and the branch's inputs and neutralized variables are unchanged. Beforeget_branch_to_change, no error was raised.get_branchor calculate in a named branch.Cost, measured locally on macOS with a load average of about 10 from other processes:
coregroup's CI resource report will show the change within the group.Only these tests were run locally, by node ID, on core 3.37.2 (from
uv.lock).make format(ruff format and ruff check)changelog.d/axiom: n/a: no policy change; this changes how four formulas remove their temporary branches.
Self-review
The author and the reviewers are all Claude sessions, so this is a self-review.
Two adversarial review rounds, each by a separate reviewer, checked the diff against the acceptance condition. Both ran the formulas on real read-only simulations from core
eeb3869b.delbecamepop. This is now refused withValueError(see above).baselinebranch. Every read-only value equals the plain value; onmainall eight raiseKeyError.Limitations the reviews reported, which this PR doesn't change:
set_inputor make arrays non-writable. So a later change that wrote into the simulation being calculated, instead of its branch, would pass these tests and fail on a real read-only simulation. No formula here does that today, and the real read-only runs pass. A test with the realmake_read_onlycan replace the stand-in once core releases it.make_read_only. If a read-only simulation already holds one of these names (an earlier calculation raised before removing its branch), the formula reuses that branch and thepopremoves the name from the read-only simulation'sbranches.main'sdeldoes the same. None of these formulas removes its branch in afinally, onmaineither.tools/tob.pyand the behavioral response measurement branches already usepop(name, None)and work on a read-only simulation. They don't use the new check, so calculated in a branch with their own names they would change the simulation being calculated (unchanged frommain).tools/period_branch.get_override_branch(the itemization branches and others) works on a read-only simulation, but finds no kept branch, so it creates a new one on every call and calculates its values again.marginal_tax_rate_helpers.pymakes the Quick Feedback selective-test runner choosetests/microsimulationandtests/policy/baseline/household, by the repository's existing mapping.🤖 Generated with Claude Code