Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Shallow copy group_entities and person_entity when cloning TBS #294

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- bump: minor
changes:
changed:
- Shallow copy GroupEntities and PopulationEntity when cloning TaxBenefitSystem object
added:
- Two tests related to extensions that were previously removed
13 changes: 13 additions & 0 deletions policyengine_core/taxbenefitsystems/tax_benefit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ def clone(self) -> "TaxBenefitSystem":
"_parameters_at_instant_cache",
"variables",
"entities",
"person_entity",
"group_entities",
):
new_dict[key] = value

Expand All @@ -676,10 +678,21 @@ def clone(self) -> "TaxBenefitSystem":
variable_name: variable.clone()
for variable_name, variable in self.variables.items()
}

# Apply shallow copies to all relevant entities
new_dict["entities"] = [copy.copy(entity) for entity in self.entities]
new_dict["person_entity"] = copy.copy(self.person_entity)
new_dict["group_entities"] = [
copy.copy(entity) for entity in self.group_entities
]

# For all shallow-copied entities, set entity._tax_benefit_system to the new system
for entity in new_dict["entities"]:
entity.set_tax_benefit_system(new)
for entity in new_dict["group_entities"]:
entity.set_tax_benefit_system(new)
new_dict["person_entity"].set_tax_benefit_system(new)

return new

def entities_plural(self) -> dict:
Expand Down
7 changes: 7 additions & 0 deletions tests/core/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def test_with_reform(tax_benefit_system):
)


def test_with_extension(tax_benefit_system):
assert (
run_yaml_test(tax_benefit_system, "test_with_extension.yaml")
== EXIT_OK
)


def test_with_anchors(tax_benefit_system):
assert (
run_yaml_test(tax_benefit_system, "test_with_anchors.yaml") == EXIT_OK
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/yaml_tests/test_with_extension.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- name: "Test using an extension"
period: 2017-01
extensions:
- policyengine_core.extension_template
input:
persons:
parent: {}
child1: {}
household:
parents: [parent]
children: [child1]
output:
local_town_child_allowance: 100

- name: "Test using an extension"
period: 2017-01
extensions:
- policyengine_core.extension_template
input:
persons:
parent: {}
child1: {}
child2: {}
household:
parents: [parent]
children: [child1, child2]
output:
local_town_child_allowance: 200
Loading