Skip to content

Commit 6484d8e

Browse files
authored
docs(entities): fix docs (#1253)
2 parents adcc161 + d94bf4d commit 6484d8e

File tree

16 files changed

+168
-90
lines changed

16 files changed

+168
-90
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### 42.0.1 [#1253](https://github.com/openfisca/openfisca-core/pull/1253)
4+
5+
#### Documentation
6+
7+
- Fix documentation of `entities`
8+
39
# 42.0.0 [#1223](https://github.com/openfisca/openfisca-core/pull/1223)
410

511
#### Breaking changes

openfisca_core/commons/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from modularizing the different components of the library, which would make
3434
them easier to test and to maintain.
3535
36-
How they could be used in a future release:
36+
How they could be used in a future release::
3737
3838
from openfisca_core import commons
3939
from openfisca_core.commons import deprecated

openfisca_core/commons/formulas.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ def apply_thresholds(
2424
choices: A list of the possible values to choose from.
2525
2626
Returns:
27-
:obj:`numpy.ndarray` of :obj:`float`:
28-
A list of the values chosen.
27+
Array[numpy.float32]: A list of the values chosen.
2928
3029
Raises:
31-
:exc:`AssertionError`: When the number of ``thresholds`` (t) and the
30+
AssertionError: When the number of ``thresholds`` (t) and the
3231
number of choices (c) are not either t == c or t == c - 1.
3332
3433
Examples:
@@ -68,8 +67,7 @@ def concat(
6867
that: Another array to concatenate.
6968
7069
Returns:
71-
:obj:`numpy.ndarray` of :obj:`numpy.str_`:
72-
An array with the concatenated values.
70+
Array[numpy.str_]: An array with the concatenated values.
7371
7472
Examples:
7573
>>> this = ["this", "that"]
@@ -101,11 +99,10 @@ def switch(
10199
value_by_condition: Values to replace for each condition.
102100
103101
Returns:
104-
:obj:`numpy.ndarray`:
105-
An array with the replaced values.
102+
Array: An array with the replaced values.
106103
107104
Raises:
108-
:exc:`AssertionError`: When ``value_by_condition`` is empty.
105+
AssertionError: When ``value_by_condition`` is empty.
109106
110107
Examples:
111108
>>> conditions = numpy.array([1, 1, 1, 2])

openfisca_core/commons/misc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def stringify_array(array: None | t.Array[numpy.generic]) -> str:
5050
array: An array.
5151
5252
Returns:
53-
:obj:`str`:
54-
"None" if the ``array`` is None, the stringified ``array`` otherwise.
53+
str: "None" if the ``array`` is None.
54+
str: The stringified ``array`` otherwise.
5555
5656
Examples:
5757
>>> import numpy
@@ -84,10 +84,11 @@ def eval_expression(
8484
"""Evaluate a string expression to a numpy array.
8585
8686
Args:
87-
expression(str): An expression to evaluate.
87+
expression: An expression to evaluate.
8888
8989
Returns:
90-
:obj:`object`: The result of the evaluation.
90+
Array: The result of the evaluation.
91+
str: The expression if it couldn't be evaluated.
9192
9293
Examples:
9394
>>> eval_expression("1 + 2")

openfisca_core/commons/rates.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ def average_rate(
2525
trim: The lower and upper bounds of the average rate.
2626
2727
Returns:
28-
:obj:`numpy.ndarray` of :obj:`float`:
29-
30-
The average rate for each target.
31-
32-
When ``trim`` is provided, values that are out of the provided bounds
33-
are replaced by :obj:`numpy.nan`.
28+
Array[numpy.float32]: The average rate for each target. When ``trim``
29+
is provided, values that are out of the provided bounds are
30+
replaced by :any:`numpy.nan`.
3431
3532
Examples:
3633
>>> target = numpy.array([1, 2, 3])
@@ -80,12 +77,9 @@ def marginal_rate(
8077
trim: The lower and upper bounds of the marginal rate.
8178
8279
Returns:
83-
:obj:`numpy.ndarray` of :obj:`float`:
84-
85-
The marginal rate for each target.
86-
87-
When ``trim`` is provided, values that are out of the provided bounds
88-
are replaced by :obj:`numpy.nan`.
80+
Array[numpy.float32]: The marginal rate for each target. When ``trim``
81+
is provided, values that are out of the provided bounds are replaced by
82+
:any:`numpy.nan`.
8983
9084
Examples:
9185
>>> target = numpy.array([1, 2, 3])

openfisca_core/entities/_core_entity.py

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010

1111

1212
class _CoreEntity:
13-
"""Base class to build entities from."""
13+
"""Base class to build entities from.
1414
15-
#: A key to identify the entity.
15+
Args:
16+
__key: A key to identify the ``_CoreEntity``.
17+
__plural: The ``key`` pluralised.
18+
__label: A summary description.
19+
__doc: A full description.
20+
*__args: Additional arguments.
21+
22+
"""
23+
24+
#: A key to identify the ``_CoreEntity``.
1625
key: t.EntityKey
1726

18-
#: The ``key``, pluralised.
27+
#: The ``key`` pluralised.
1928
plural: t.EntityPlural
2029

2130
#: A summary description.
@@ -24,10 +33,10 @@ class _CoreEntity:
2433
#: A full description.
2534
doc: str
2635

27-
#: Whether the entity is a person or not.
36+
#: Whether the ``_CoreEntity`` is a person or not.
2837
is_person: ClassVar[bool]
2938

30-
#: A TaxBenefitSystem instance.
39+
#: A ``TaxBenefitSystem`` instance.
3140
_tax_benefit_system: None | t.TaxBenefitSystem = None
3241

3342
@abc.abstractmethod
@@ -44,15 +53,30 @@ def __repr__(self) -> str:
4453
return f"{self.__class__.__name__}({self.key})"
4554

4655
def set_tax_benefit_system(self, tax_benefit_system: t.TaxBenefitSystem) -> None:
47-
"""An Entity belongs to a TaxBenefitSystem."""
56+
"""A ``_CoreEntity`` belongs to a ``TaxBenefitSystem``."""
4857
self._tax_benefit_system = tax_benefit_system
4958

5059
def get_variable(
5160
self,
5261
variable_name: t.VariableName,
5362
check_existence: bool = False,
5463
) -> t.Variable | None:
55-
"""Get a ``variable_name`` from ``variables``."""
64+
"""Get ``variable_name`` from ``variables``.
65+
66+
Args:
67+
variable_name: The ``Variable`` to be found.
68+
check_existence: Was the ``Variable`` found?
69+
70+
Returns:
71+
Variable: When the ``Variable`` exists.
72+
None: When the ``Variable`` doesn't exist.
73+
74+
Raises:
75+
ValueError: When ``check_existence`` is ``True`` and
76+
the ``Variable`` doesn't exist.
77+
78+
"""
79+
5680
if self._tax_benefit_system is None:
5781
msg = "You must set 'tax_benefit_system' before calling this method."
5882
raise ValueError(
@@ -61,7 +85,21 @@ def get_variable(
6185
return self._tax_benefit_system.get_variable(variable_name, check_existence)
6286

6387
def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> None:
64-
"""Check if ``variable_name`` is defined for ``self``."""
88+
"""Check if ``variable_name`` is defined for ``self``.
89+
90+
Args:
91+
variable_name: The ``Variable`` to be found.
92+
93+
Returns:
94+
Variable: When the ``Variable`` exists.
95+
None: When the :attr:`_tax_benefit_system` is not set.
96+
97+
Raises:
98+
ValueError: When the ``Variable`` exists but is defined
99+
for another ``Entity``.
100+
101+
"""
102+
65103
entity: None | t.CoreEntity = None
66104
variable: None | t.Variable = self.get_variable(
67105
variable_name,
@@ -86,7 +124,16 @@ def check_variable_defined_for_entity(self, variable_name: t.VariableName) -> No
86124

87125
@staticmethod
88126
def check_role_validity(role: object) -> None:
89-
"""Check if a ``role`` is an instance of Role."""
127+
"""Check if ``role`` is an instance of ``Role``.
128+
129+
Args:
130+
role: Any object.
131+
132+
Raises:
133+
ValueError: When ``role`` is not a ``Role``.
134+
135+
"""
136+
90137
if role is not None and not isinstance(role, Role):
91138
msg = f"{role} is not a valid role"
92139
raise ValueError(msg)

openfisca_core/entities/_description.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@dataclasses.dataclass(frozen=True)
88
class _Description:
9-
r"""A Role's description.
9+
"""A ``Role``'s description.
1010
1111
Examples:
1212
>>> data = {
@@ -35,10 +35,10 @@ class _Description:
3535
3636
"""
3737

38-
#: A key to identify the Role.
38+
#: A key to identify the ``Role``.
3939
key: str
4040

41-
#: The ``key``, pluralised.
41+
#: The ``key`` pluralised.
4242
plural: None | str = None
4343

4444
#: A summary description.

openfisca_core/entities/entity.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,29 @@
77

88

99
class Entity(_CoreEntity):
10-
"""An entity (e.g. a person, a household) on which calculations can be run."""
10+
"""An entity (e.g. a person, a household) on which calculations can be run.
1111
12-
#: Whether the entity is a person or not.
12+
Args:
13+
key: A key to identify the ``Entity``.
14+
plural: The ``key`` pluralised.
15+
label: A summary description.
16+
doc: A full description.
17+
18+
"""
19+
20+
#: A key to identify the ``Entity``.
21+
key: t.EntityKey
22+
23+
#: The ``key`` pluralised.
24+
plural: t.EntityPlural
25+
26+
#: A summary description.
27+
label: str
28+
29+
#: A full description.
30+
doc: str
31+
32+
#: Whether the ``Entity`` is a person or not.
1333
is_person: ClassVar[bool] = True
1434

1535
def __init__(self, key: str, plural: str, label: str, doc: str) -> None:

openfisca_core/entities/group_entity.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,35 @@
1414
class GroupEntity(_CoreEntity):
1515
"""Represents an entity containing several others with different roles.
1616
17-
A :class:`.GroupEntity` represents an :class:`.Entity` containing
18-
several other :class:`.Entity` with different :class:`.Role`, and on
19-
which calculations can be run.
17+
A ``GroupEntity`` represents an ``Entity`` containing several other entities,
18+
with different roles, and on which calculations can be run.
2019
2120
Args:
22-
key: A key to identify the group entity.
23-
plural: The ``key``, pluralised.
21+
key: A key to identify the ``GroupEntity``.
22+
plural: The ``key`` pluralised.
2423
label: A summary description.
2524
doc: A full description.
26-
roles: The list of :class:`.Role` of the group entity.
25+
roles: The list of roles of the group entity.
2726
containing_entities: The list of keys of group entities whose members
2827
are guaranteed to be a superset of this group's entities.
2928
3029
"""
3130

31+
#: A key to identify the ``Entity``.
32+
key: t.EntityKey
33+
34+
#: The ``key`` pluralised.
35+
plural: t.EntityPlural
36+
37+
#: A summary description.
38+
label: str
39+
40+
#: A full description.
41+
doc: str
42+
43+
#: The list of roles of the ``GroupEntity``.
44+
roles: Iterable[Role]
45+
3246
#: Whether the entity is a person or not.
3347
is_person: ClassVar[bool] = False
3448

0 commit comments

Comments
 (0)