Shallow copy entities when cloning TaxBenefitSystem
instances
#288
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 #287.
When applying a reform,
-core
creates a newTaxBenefitSystem
instance by using theTaxBenefitSystem.clone()
method upon the current-law system. Previously, when doing so, it assigned the old entities by reference to the newTaxBenefitSystem
instance, thereby merely linking them to the new system.This was alright when running simulations that update variables or parameters, as those are left out of the clone, then added later. However, with reforms that contain a
neutralize_variable
call, theis_neutralized
value is attached to the variable that is stored as part of the entity.When a user on the front end would access the "Varying your earnings" component for a reform like the New York WFTC, which contains a
neutralize_variable
call, the following would occur:TaxBenefitSystem.clone()
would be used to create a new TBS. At this point, the entities are the same as in the default system, but linked to the new TBS. Its variable data would be incorrect, and thus incorrectly fed to the TBS'sPopulation
and eachPopulation
'sHolder
instances. However, this variable is neutralized correctly in theSimulation
, and when calculating a variable, theSimulation
checks for whether it's neutralized within theSimulation
's TBS before trying to find a pre-calculated value.Simulation
would attempt to calculate and check if the relevant population's holder contains a pre-calculated value. The holder would then would check if its variable is neutralized, think it is, and return an array of 0's.This reform instead adds a Python shallow copy, preventing the entities from referring to stale
TaxBenefitSystem
instances.This bug surfaced because of how the API calculates the "Varying your earnings" component, which would make it easier to test within the API. I will be adding a test there, which should be merged after this PR. Until then, this will remain in draft.