Skip to content

Commit edad7ad

Browse files
Fix Scenario backwards compatibility (#1317)
* Fix Scenario backwards compatibility Enhances the Scenario class to support all Reform object types including instances and callable objects. This maintains backwards compatibility with existing code that uses various Reform representations. * Fix bug * Remove duplicate VAT entry
1 parent d8e3aaa commit edad7ad

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: patch
2+
changes:
3+
fixed:
4+
- Scenario class now supports all Reform object types and maintains backwards compatibility.

policyengine_uk/utils/scenario.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional, Callable, Dict, Type, Union
33
from policyengine_core.simulations import Simulation
44
from policyengine_core.reforms import Reform
5+
from policyengine_core.periods import period, instant
56

67

78
class Scenario(BaseModel):
@@ -111,8 +112,42 @@ def modifier(simulation: Simulation) -> None:
111112

112113
elif isinstance(reform, dict):
113114
# Dictionary of parameter changes
114-
return cls(
115-
parameter_changes=reform,
115+
# Make sure to capture YYYY-MM-DD.YYYY-MM-DD.
116+
117+
def modifier(sim: Simulation):
118+
for parameter in reform:
119+
if isinstance(reform[parameter], dict):
120+
for period_str, value in reform[parameter].items():
121+
if "." in period_str:
122+
start = instant(period_str.split(".")[0])
123+
stop = instant(period_str.split(".")[1])
124+
period_ = None
125+
else:
126+
period_ = period(period_str)
127+
sim.tax_benefit_system.parameters.get_child(
128+
parameter
129+
).update(
130+
start=start,
131+
stop=stop,
132+
period=period_,
133+
value=value,
134+
)
135+
else:
136+
start = instant("2023-01-01")
137+
stop = None
138+
period_ = None
139+
140+
sim.tax_benefit_system.parameters.get_child(
141+
parameter
142+
).update(
143+
start=start,
144+
stop=stop,
145+
period=period_,
146+
value=reform[parameter],
147+
)
148+
149+
return Scenario(
150+
simulation_modifier=modifier,
116151
)
117152

118153
elif isinstance(reform, tuple):

policyengine_uk/variables/gov/gov_tax.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class gov_tax(Variable):
2626
"national_insurance",
2727
"LVT",
2828
"carbon_tax",
29-
"vat_change",
3029
"capital_gains_tax",
3130
"private_school_vat",
3231
"corporate_incident_tax_revenue_change",

0 commit comments

Comments
 (0)