|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections.abc import Mapping |
3 | 4 | from typing import ClassVar |
4 | 5 |
|
5 | 6 | import abc |
@@ -52,6 +53,64 @@ def __init__(self, *__args: object, **__kwargs: object) -> None: ... |
52 | 53 | def __repr__(self) -> str: |
53 | 54 | return f"{self.__class__.__name__}({self.key})" |
54 | 55 |
|
| 56 | + @property |
| 57 | + def variables(self, /) -> Mapping[t.VariableName, t.Variable]: |
| 58 | + """Get all variables defined for the entity. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + dict[str, Variable]: The variables defined for the entity. |
| 62 | +
|
| 63 | + Raises: |
| 64 | + ValueError: When the :attr:`.tax_benefit_system` is not set yet. |
| 65 | +
|
| 66 | + Examples: |
| 67 | + >>> from openfisca_core import ( |
| 68 | + ... entities, |
| 69 | + ... periods, |
| 70 | + ... taxbenefitsystems, |
| 71 | + ... variables, |
| 72 | + ... ) |
| 73 | +
|
| 74 | + >>> this = entities.SingleEntity("this", "", "", "") |
| 75 | + >>> that = entities.SingleEntity("that", "", "", "") |
| 76 | +
|
| 77 | + >>> this.variables |
| 78 | + Traceback (most recent call last): |
| 79 | + ValueError: You must set 'tax_benefit_system' to call this method. |
| 80 | +
|
| 81 | + >>> tbs = taxbenefitsystems.TaxBenefitSystem([this, that]) |
| 82 | + >>> this, that = tbs.entities |
| 83 | +
|
| 84 | + >>> this.variables |
| 85 | + {} |
| 86 | +
|
| 87 | + >>> that.variables |
| 88 | + {} |
| 89 | +
|
| 90 | + >>> class tax(variables.Variable): |
| 91 | + ... definition_period = periods.MONTH |
| 92 | + ... value_type = float |
| 93 | + ... entity = that |
| 94 | +
|
| 95 | + >>> tbs.add_variable(tax) |
| 96 | + <openfisca_core.entities._core_entity.tax object at ...> |
| 97 | +
|
| 98 | + >>> this.variables |
| 99 | + {} |
| 100 | +
|
| 101 | + >>> that.variables |
| 102 | + {'tax': <openfisca_core.entities._core_entity.tax object at ...>} |
| 103 | +
|
| 104 | + """ |
| 105 | + if self.tax_benefit_system is None: |
| 106 | + msg = "You must set 'tax_benefit_system' to call this method." |
| 107 | + raise ValueError(msg) |
| 108 | + return { |
| 109 | + name: variable |
| 110 | + for name, variable in self.tax_benefit_system.variables.items() |
| 111 | + if variable.entity.key == self.key |
| 112 | + } |
| 113 | + |
55 | 114 | def get_variable( |
56 | 115 | self, |
57 | 116 | variable_name: t.VariableName, |
|
0 commit comments