44from typing import ClassVar
55
66import abc
7- import os
87
98from . import types as t
109from ._errors import TaxBenefitSystemUnsetError , VariableNotFoundError
@@ -103,6 +102,9 @@ def variables(self, /) -> Mapping[t.VariableName, t.Variable]:
103102 >>> that.variables
104103 {'tax': <openfisca_core.entities._core_entity.tax object at ...>}
105104
105+ >>> that.variables["tax"]
106+ <openfisca_core.entities._core_entity.tax object at ...>
107+
106108 """
107109 if self .tax_benefit_system is None :
108110 raise TaxBenefitSystemUnsetError
@@ -115,6 +117,7 @@ def variables(self, /) -> Mapping[t.VariableName, t.Variable]:
115117 def get_variable (
116118 self ,
117119 variable_name : t .VariableName ,
120+ / ,
118121 check_existence : bool = False ,
119122 ) -> None | t .Variable :
120123 """Get ``variable_name`` from ``variables``.
@@ -173,81 +176,11 @@ def get_variable(
173176 """
174177 if self .tax_benefit_system is None :
175178 raise TaxBenefitSystemUnsetError
176- if not check_existence :
177- return self .variables .get (variable_name )
178- try :
179- return self .variables [variable_name ]
180- except KeyError as error :
181- raise VariableNotFoundError (variable_name , self .plural ) from error
182-
183- def check_variable_defined_for_entity (self , variable_name : t .VariableName ) -> None :
184- """Check if ``variable_name`` is defined for ``self``.
185-
186- Args:
187- variable_name: The ``Variable`` to be found.
188-
189- Raises:
190- ValueError: When the ``Variable`` exists but is defined
191- for another ``Entity``.
192-
193- Examples:
194- >>> from openfisca_core import (
195- ... entities,
196- ... periods,
197- ... taxbenefitsystems,
198- ... variables,
199- ... )
200-
201- >>> this = entities.SingleEntity("this", "", "", "")
202- >>> that = entities.SingleEntity("that", "", "", "")
203- >>> tax_benefit_system = taxbenefitsystems.TaxBenefitSystem([that])
204- >>> this.tax_benefit_system = tax_benefit_system
205-
206- >>> this.check_variable_defined_for_entity("tax")
207- Traceback (most recent call last):
208- VariableNotFoundError: You tried to calculate or to set a value...
209-
210- >>> class tax(variables.Variable):
211- ... definition_period = periods.WEEK
212- ... value_type = int
213- ... entity = that
214-
215- >>> this.tax_benefit_system.add_variable(tax)
216- <openfisca_core.entities._core_entity.tax object at ...>
217-
218- >>> this.check_variable_defined_for_entity("tax")
219- Traceback (most recent call last):
220- ValueError: You tried to compute the variable 'tax' for the enti...
221-
222- >>> tax.entity = this
223-
224- >>> this.tax_benefit_system.update_variable(tax)
225- <openfisca_core.entities._core_entity.tax object at ...>
226-
227- >>> this.check_variable_defined_for_entity("tax")
228-
229- """
230- entity : None | t .CoreEntity = None
231- variable : None | t .Variable = self .get_variable (
232- variable_name ,
233- check_existence = True ,
234- )
235-
236- if variable is not None :
237- entity = variable .entity
238-
239- if entity is None :
240- return
241-
242- if entity .key != self .key :
243- message = (
244- f"You tried to compute the variable '{ variable_name } ' for" ,
245- f"the entity '{ self .plural } '; however the variable" ,
246- f"'{ variable_name } ' is defined for '{ entity .plural } '." ,
247- "Learn more about entities in our documentation:" ,
248- "<https://openfisca.org/doc/coding-the-legislation/50_entities.html>." ,
249- )
250- raise ValueError (os .linesep .join (message ))
179+ if (variable := self .variables .get (variable_name )) is not None :
180+ return variable
181+ if check_existence :
182+ raise VariableNotFoundError (variable_name , self .plural )
183+ return None
251184
252185
253186__all__ = ["CoreEntity" ]
0 commit comments