Skip to content
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
1 change: 1 addition & 0 deletions changelog.d/exempt-inherited-computation-modes.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Exempt attributes inherited from a baseline variable (via reform `update_variable`) from the exclusive computation-mode check. The check now applies only to modes a variable class declares itself, so reforms may redeclare a formula variable with `adds`/`subtracts` while inherited baseline formulas keep their runtime precedence. Runtime `uprating` assignments still count as explicit declarations.
36 changes: 35 additions & 1 deletion policyengine_core/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def __init__(self, baseline_variable=None):
for name, value in self.__class__.__dict__.items()
if not name.startswith("__")
}
# Attributes the class declares itself, as opposed to attributes
# inherited from a baseline variable when a reform updates it.
# Computation-mode checks only apply to explicit declarations.
self._explicit_attribute_names = frozenset(
name for name, value in attr.items() if value is not None
)

# Allow inheritance for some properties
INHERITED_ALLOWED_PROPERTIES = (
Expand Down Expand Up @@ -338,12 +344,19 @@ def uprating(self):
@uprating.setter
def uprating(self, value):
old_value = getattr(self, "_uprating", None)
old_explicit = getattr(self, "_explicit_attribute_names", frozenset())
self._uprating = value
# During __init__ (before formulas exist) the assignment may carry a
# value inherited from a baseline variable; only runtime assignments
# count as explicit declarations.
if hasattr(self, "formulas"):
if value is not None:
self._explicit_attribute_names = old_explicit | {"uprating"}
try:
self.check_computation_modes()
except ValueError:
self._uprating = old_value
self._explicit_attribute_names = old_explicit
raise

def get_computation_modes(self):
Expand All @@ -356,8 +369,29 @@ def get_computation_modes(self):
computation_modes.append("uprating")
return computation_modes

def get_explicit_computation_modes(self):
"""Computation modes the class declares itself.

Excludes attributes inherited from a baseline variable when a
reform updates it: a reform may, for example, redeclare a formula
variable with ``adds``, and the inherited baseline formulas keep
their runtime precedence rather than constituting a mixed-mode
authoring error.
"""
explicit = getattr(self, "_explicit_attribute_names", None)
if explicit is None:
return self.get_computation_modes()
computation_modes = []
if any(name.startswith(config.FORMULA_NAME_PREFIX) for name in explicit):
computation_modes.append("formula")
if "adds" in explicit or "subtracts" in explicit:
computation_modes.append("adds/subtracts")
if "uprating" in explicit:
computation_modes.append("uprating")
return computation_modes

def check_computation_modes(self):
computation_modes = self.get_computation_modes()
computation_modes = self.get_explicit_computation_modes()
if len(computation_modes) > 1:
raise ValueError(
f'Variable "{self.name}" mixes computation modes: '
Expand Down
36 changes: 36 additions & 0 deletions tests/core/test_reforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,42 @@ def apply(self):
assert disposable_income2 > 100


def test_update_variable_from_formula_to_adds(tax_benefit_system):
"""Updating a formula variable with an adds declaration is not a
mixed-mode authoring error: the baseline formulas are inherited and
keep their runtime precedence."""

class disposable_income(Variable):
adds = ["salary"]

class reform(Reform):
def apply(self):
self.update_variable(disposable_income)

reformed = reform(tax_benefit_system)
updated = reformed.get_variable("disposable_income")

assert updated.adds == ["salary"]
# Baseline formulas are kept for periods before the update.
assert len(updated.formulas) > 0
assert updated.get_explicit_computation_modes() == ["adds/subtracts"]


def test_update_variable_with_explicit_mixed_modes_raises(tax_benefit_system):
class disposable_income(Variable):
adds = ["salary"]

def formula(person, period):
return person.empty_array()

class reform(Reform):
def apply(self):
self.update_variable(disposable_income)

with pytest.raises(ValueError, match="mixes computation modes"):
reform(tax_benefit_system)


def test_replace_variable(tax_benefit_system):
class disposable_income(Variable):
definition_period = MONTH
Expand Down
Loading