Repository navigation
Add Simulation.temporary_branch for a branch used by one block of code - #677
Merged
Merged
Conversation
A formula that needs a changed copy of its simulation creates a branch, calculates in it, and should leave nothing behind. The tests describe a context manager that keeps the branch in a plain simulation's branches only inside its block, removes it when the block ends or raises, never keeps it in a read-only simulation, and refuses with ValueError a name the simulation already holds or is named. They fail before the change because the method does not exist. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A context manager that gives a new branch, made by get_branch, for one block. A plain simulation holds it in branches only while the block runs and removes it when the block ends or raises; a read-only simulation never keeps it. A name the simulation already holds, or its own branch name, raises ValueError before anything changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found no bug, but two cases were covered only by reasoning: a formula that opens a temporary branch while it calculates, on a plain and a read-only simulation, and a subclass whose get_branch override must make the branch. The docs now say that system or parameter changes need clone_system=True, that a branch the block put under the same name stays, and which names are refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found that a get_branch override raising after core has registered the branch leaves it in branches, so the next block with that name raises ValueError instead. The test fails on that assertion. A second test covers a block that raises on a read-only simulation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
temporary_branch checks the name is free before calling get_branch, so an entry under it after get_branch raised was registered by that call. Remove it before re-raising. Also say in the docstring's Raises entry and the read-only API notes which names are refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #676
What and why
Some country-package formulas need a changed copy of their simulation for one calculation, then remove it. policyengine-us has four: its marginal tax rate helper, the capital gains marginal tax rate, and the employer Social Security and Medicare income tax revenue variables. Each calls
get_branch(name), calculates in the branch, then runsdel simulation.branches[name]. On a read-only simulation (#672),get_branchwith a new name doesn't keep the branch inbranches, so thatdelraisesKeyError. And when the calculation inside the branch raises, the branch stays registered, and the nextget_branchwith that name returns it with the failed calculation's inputs.This adds
Simulation.temporary_branch(name="branch", clone_system=False), a context manager:get_branch, so a subclass'sget_branchoverride still runs, and the branch is part of the simulation as any other branch is.branchesonly while the block runs. When the block ends, normally or by raising, the branch is removed; the exception still reaches the caller. If the block removed the branch itself or put another branch under the same name, that one stays. If making the branch fails (aget_branchoverride raising after core registered it), nothing is left under its name.branches, or its ownbranch_name, raisesValueErrorbefore anything changes:get_branchwould return that branch, or the simulation itself, which the block would then change and remove.Docs: a "Temporary branches" section in
docs/usage/branches.md, a bullet in the read-only list ofdocs/python_api/simulations.md, and the method's docstring (also rendered by the API page'sautoclass). Changelog:changelog.d/temporary-branch.added.md.A policyengine-us PR will move the four formulas onto this once it is released.
Tests
tests/core/test_temporary_branch.py, 16 tests on the country template:get_branchoverride that raises after core registered the branch leaves nothing registered;clone_system=Truegives the branch its own system;ValueErrorwithout running the block or changing the held branch;get_branchoverride makes the branch and receivesclone_system.Results, run by node ID only:
0dcf31eb). All 11 failed there becauseSimulation.temporary_branchdidn't exist, and all pass aftera4a7f1e5.b8612901): the formula test, which runs in two cases, and the override test. They pass.00b0e18a). The failed-creation test failed there on its assertion, because the branch stayed registered, and passes afterc3f74da4. The read-only raise test adds coverage and passed from the start.uv.lock.ruff formatandruff checkpass on the changed Python files.mypy0.991 from the lock file stops in this environment, asking for thetyped_astpackage, so it wasn't run; CI doesn't run it.Review
Two adversarial review rounds, each by a separate reviewer, found no bug. Round 1 checked:
return;It found two cases covered only by reasoning and some missing documentation; the last commit adds those tests and fixes the docs:
get_branchoverride;clone_system=Truefor system or parameter changes;Round 2 also checked the description's claims and ran six existing tests, by node ID, that delete branches during re-runs and branch read-only simulations; all passed. It checked:
It raised three points, now addressed:
get_branchoverride raises after core registered it (fixed, with a test that failed first;get_branchfollowed bydelleaks the same way today);Limitations it reported, which this PR doesn't change:
get_branch's docstring already says that a branch, like the rest of a simulation, isn't safe to read from several threads at once.ValueError.The author and the reviewers are all Claude sessions, so this is a self-review.
Merge
Every earlier task this builds on has merged; it branches from
masterat8eabab83. The requester gave the go-ahead to merge once the checks pass.🤖 Generated with Claude Code