Skip to content

Commit 6af247e

Browse files
committed
BREAKING CHANGE: remove CoreEntity.set_tax_benefit_system
The feature is now provided by simple variable assignment: ```python entity.tax_benefit_system = tax_benefit_system ``` Likewise, `entity.tax_benefit_system` becomes part of the public API (which implicitly it was as the private marker was being violated).
1 parent 2fe9d10 commit 6af247e

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

openfisca_core/entities/_core_entity.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,14 @@ class CoreEntity:
4444
is_person: ClassVar[bool]
4545

4646
#: A ``TaxBenefitSystem`` instance.
47-
_tax_benefit_system: None | t.TaxBenefitSystem = None
47+
tax_benefit_system: None | t.TaxBenefitSystem = None
4848

4949
@abc.abstractmethod
5050
def __init__(self, *__args: object, **__kwargs: object) -> None: ...
5151

5252
def __repr__(self) -> str:
5353
return f"{self.__class__.__name__}({self.key})"
5454

55-
def set_tax_benefit_system(self, tax_benefit_system: t.TaxBenefitSystem) -> None:
56-
"""A ``CoreEntity`` belongs to a ``TaxBenefitSystem``."""
57-
self._tax_benefit_system = tax_benefit_system
58-
5955
def get_variable(
6056
self,
6157
variable_name: t.VariableName,
@@ -92,7 +88,7 @@ def get_variable(
9288
ValueError: You must set 'tax_benefit_system' before calling this...
9389
9490
>>> tax_benefit_system = taxbenefitsystems.TaxBenefitSystem([this])
95-
>>> this.set_tax_benefit_system(tax_benefit_system)
91+
>>> this.tax_benefit_system = tax_benefit_system
9692
9793
>>> this.get_variable("tax")
9894
@@ -105,19 +101,18 @@ def get_variable(
105101
... value_type = float
106102
... entity = that
107103
108-
>>> this._tax_benefit_system.add_variable(tax)
104+
>>> this.tax_benefit_system.add_variable(tax)
109105
<openfisca_core.entities._core_entity.tax object at ...>
110106
111107
>>> this.get_variable("tax")
112108
<openfisca_core.entities._core_entity.tax object at ...>
113109
110+
114111
"""
115-
if self._tax_benefit_system is None:
116-
msg = "You must set 'tax_benefit_system' before calling this method."
117-
raise ValueError(
118-
msg,
119-
)
120-
return self._tax_benefit_system.get_variable(variable_name, check_existence)
112+
if self.tax_benefit_system is None:
113+
msg = "You must set 'tax_benefit_system' to call this method."
114+
raise ValueError(msg)
115+
return self.tax_benefit_system.get_variable(variable_name, check_existence)
121116

122117
def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> None:
123118
"""Check if ``variable_name`` is defined for ``self``.
@@ -140,7 +135,7 @@ def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> No
140135
>>> this = entities.SingleEntity("this", "", "", "")
141136
>>> that = entities.SingleEntity("that", "", "", "")
142137
>>> tax_benefit_system = taxbenefitsystems.TaxBenefitSystem([that])
143-
>>> this.set_tax_benefit_system(tax_benefit_system)
138+
>>> this.tax_benefit_system = tax_benefit_system
144139
145140
>>> this.check_variable_defined_for_entity("tax")
146141
Traceback (most recent call last):
@@ -151,7 +146,7 @@ def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> No
151146
... value_type = int
152147
... entity = that
153148
154-
>>> this._tax_benefit_system.add_variable(tax)
149+
>>> this.tax_benefit_system.add_variable(tax)
155150
<openfisca_core.entities._core_entity.tax object at ...>
156151
157152
>>> this.check_variable_defined_for_entity("tax")
@@ -160,7 +155,7 @@ def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> No
160155
161156
>>> tax.entity = this
162157
163-
>>> this._tax_benefit_system.update_variable(tax)
158+
>>> this.tax_benefit_system.update_variable(tax)
164159
<openfisca_core.entities._core_entity.tax object at ...>
165160
166161
>>> this.check_variable_defined_for_entity("tax")

openfisca_core/populations/_core_population.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __call__(
8787
>>> population("salary", period)
8888
8989
>>> tbs = taxbenefitsystems.TaxBenefitSystem([person])
90-
>>> person.set_tax_benefit_system(tbs)
90+
>>> person.tax_benefit_system = tbs
9191
>>> simulation = simulations.Simulation(tbs, {person.key: population})
9292
>>> population("salary", period)
9393
Traceback (most recent call last):
@@ -365,7 +365,7 @@ def get_holder(self, variable_name: t.VariableName) -> t.Holder:
365365
... value_type = int
366366
367367
>>> tbs = taxbenefitsystems.TaxBenefitSystem([person])
368-
>>> person.set_tax_benefit_system(tbs)
368+
>>> person.tax_benefit_system = tbs
369369
>>> population = populations.SinglePopulation(person)
370370
>>> simulation = simulations.Simulation(tbs, {person.key: population})
371371
>>> population.get_holder("income_tax")

openfisca_core/taxbenefitsystems/tax_benefit_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, entities: Sequence[Entity]) -> None:
7272
entity for entity in self.entities if not entity.is_person
7373
]
7474
for entity in self.entities:
75-
entity.set_tax_benefit_system(self)
75+
entity.tax_benefit_system = self
7676

7777
@property
7878
def base_tax_benefit_system(self):
@@ -572,7 +572,7 @@ def clone(self):
572572
):
573573
new_dict[key] = value
574574
for entity in new_dict["entities"]:
575-
entity.set_tax_benefit_system(new)
575+
entity.tax_benefit_system = new
576576

577577
new_dict["parameters"] = self.parameters.clone()
578578
new_dict["_parameters_at_instant_cache"] = {}

openfisca_core/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ def get_population(self, plural: None | str, /) -> CorePopulation: ...
262262
class TaxBenefitSystem(Protocol):
263263
person_entity: SingleEntity
264264

265+
@property
266+
def variables(self, /) -> dict[VariableName, Variable]: ...
265267
def get_variable(
266268
self,
267269
variable_name: VariableName,

tests/core/tools/test_runner/test_yaml_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TaxBenefitSystem:
1515
def __init__(self) -> None:
1616
self.variables = {"salary": TestVariable()}
1717
self.person_entity = Entity("person", "persons", None, "")
18-
self.person_entity.set_tax_benefit_system(self)
18+
self.person_entity.tax_benefit_system = self
1919

2020
def get_package_metadata(self):
2121
return {"name": "Test", "version": "Test"}

0 commit comments

Comments
 (0)