Skip to content

Commit 7d4933a

Browse files
authored
Fix docstrings
2 parents 7c56423 + 691090e commit 7d4933a

9 files changed

Lines changed: 73 additions & 57 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Changelog
22

3+
### 35.8.4 [#1131](https://github.com/openfisca/openfisca-core/pull/1131)
4+
5+
#### Technical changes
6+
7+
- Correct some type hints and docstrings.
8+
39
### 35.8.3 [#1127](https://github.com/openfisca/openfisca-core/pull/1127)
410

511
#### Technical changes
612

7-
- Fix the build for Anaconda in CI. The conda build failed on master because of a replacement in a comment string.
13+
- Fix the build for Anaconda in CI. The conda build failed on master because of a replacement in a comment string.
814
- The _ were removed in the comment to avoid a replace.
915

1016
### 35.8.2 [#1128](https://github.com/openfisca/openfisca-core/pull/1128)

openfisca_core/parameters/parameter.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
from __future__ import annotations
2+
3+
from typing import Dict, List, Optional
4+
15
import copy
26
import os
3-
import typing
47

58
from openfisca_core import commons, periods
69
from openfisca_core.errors import ParameterParsingError
710
from openfisca_core.parameters import config, helpers, AtInstantLike, ParameterAtInstant
811

912

1013
class Parameter(AtInstantLike):
11-
"""
12-
A parameter of the legislation. Parameters can change over time.
14+
"""A parameter of the legislation.
1315
14-
:param string name: Name of the parameter, e.g. "taxes.some_tax.some_param"
15-
:param dict data: Data loaded from a YAML file.
16-
:param string file_path: File the parameter was loaded from.
17-
:param string documentation: Documentation describing parameter usage and context.
16+
Parameters can change over time.
1817
18+
Attributes:
19+
values_list: List of the values, in reverse chronological order.
20+
21+
22+
Args:
23+
name: Name of the parameter, e.g. "taxes.some_tax.some_param".
24+
data: Data loaded from a YAML file.
25+
file_path: File the parameter was loaded from.
1926
2027
Instantiate a parameter without metadata:
2128
@@ -34,18 +41,15 @@ class Parameter(AtInstantLike):
3441
}
3542
})
3643
37-
.. attribute:: values_list
38-
39-
List of the values, in reverse chronological order
4044
"""
4145

42-
def __init__(self, name, data, file_path = None):
46+
def __init__(self, name: str, data: dict, file_path: Optional[str] = None) -> None:
4347
self.name: str = name
44-
self.file_path: str = file_path
48+
self.file_path: Optional[str] = file_path
4549
helpers._validate_parameter(self, data, data_type = dict)
46-
self.description: str = None
47-
self.metadata: typing.Dict = {}
48-
self.documentation: str = None
50+
self.description: Optional[str] = None
51+
self.metadata: Dict = {}
52+
self.documentation: Optional[str] = None
4953
self.values_history = self # Only for backward compatibility
5054

5155
# Normal parameter declaration: the values are declared under the 'values' key: parse the description and metadata.
@@ -85,7 +89,7 @@ def __init__(self, name, data, file_path = None):
8589
value_at_instant = ParameterAtInstant(value_name, instant_str, data = instant_info, file_path = self.file_path, metadata = self.metadata)
8690
values_list.append(value_at_instant)
8791

88-
self.values_list: typing.List[ParameterAtInstant] = values_list
92+
self.values_list: List[ParameterAtInstant] = values_list
8993

9094
def __repr__(self):
9195
return os.linesep.join([

openfisca_core/parameters/parameter_at_instant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class ParameterAtInstant:
1616

1717
def __init__(self, name, instant_str, data = None, file_path = None, metadata = None):
1818
"""
19-
:param string name: name of the parameter, e.g. "taxes.some_tax.some_param"
20-
:param string instant_str: Date of the value in the format `YYYY-MM-DD`.
19+
:param str name: name of the parameter, e.g. "taxes.some_tax.some_param"
20+
:param str instant_str: Date of the value in the format `YYYY-MM-DD`.
2121
:param dict data: Data, usually loaded from a YAML file.
2222
"""
2323
self.name: str = name

openfisca_core/parameters/parameter_node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def __init__(self, name = "", directory_path = None, data = None, file_path = No
1919
"""
2020
Instantiate a ParameterNode either from a dict, (using `data`), or from a directory containing YAML files (using `directory_path`).
2121
22-
:param string name: Name of the node, eg "taxes.some_tax".
23-
:param string directory_path: Directory containing YAML files describing the node.
22+
:param str name: Name of the node, eg "taxes.some_tax".
23+
:param str directory_path: Directory containing YAML files describing the node.
2424
:param dict data: Object representing the parameter node. It usually has been extracted from a YAML file.
25-
:param string file_path: YAML file from which the `data` has been extracted from.
25+
:param str file_path: YAML file from which the `data` has been extracted from.
2626
2727
2828
Instantiate a ParameterNode from a dict:

openfisca_core/reforms/reform.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
from __future__ import annotations
2+
13
import copy
24

35
from openfisca_core.parameters import ParameterNode
46
from openfisca_core.taxbenefitsystems import TaxBenefitSystem
57

68

79
class Reform(TaxBenefitSystem):
8-
"""
9-
A modified TaxBenefitSystem
10+
"""A modified TaxBenefitSystem
1011
12+
All reforms must subclass `Reform` and implement a method `apply()`.
1113
12-
All reforms must subclass `Reform` and implement a method `apply()`.
13-
14-
In this method, the reform can add or replace variables and call `modify_parameters` to modify the parameters of the legislation.
14+
In this method, the reform can add or replace variables and call `modify_parameters` to modify the parameters of the legislation.
1515
1616
Example:
1717
@@ -64,12 +64,12 @@ def full_key(self):
6464
return key
6565

6666
def modify_parameters(self, modifier_function):
67-
"""
68-
Make modifications on the parameters of the legislation
67+
"""Make modifications on the parameters of the legislation.
6968
7069
Call this function in `apply()` if the reform asks for legislation parameter modifications.
7170
72-
:param modifier_function: A function that takes an object of type :any:`ParameterNode` and should return an object of the same type.
71+
Args:
72+
modifier_function: A function that takes a :obj:`.ParameterNode` and should return an object of the same type.
7373
"""
7474
baseline_parameters = self.baseline.parameters
7575
baseline_parameters_copy = copy.deepcopy(baseline_parameters)

openfisca_core/taxbenefitsystems/tax_benefit_system.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from __future__ import annotations
2+
3+
from typing import Any, Dict, Optional, Sequence
4+
15
import copy
26
import glob
37
import importlib
@@ -27,28 +31,27 @@ class TaxBenefitSystem:
2731
2832
It stores parameters (values defined for everyone) and variables (values defined for some given entity e.g. a person).
2933
30-
:param entities: Entities used by the tax benefit system.
31-
:param string parameters: Directory containing the YAML parameter files.
32-
34+
Attributes:
35+
parameters: Directory containing the YAML parameter files.
3336
34-
.. attribute:: parameters
37+
Args:
38+
entities: Entities used by the tax benefit system.
3539
36-
:obj:`.ParameterNode` containing the legislation parameters
3740
"""
3841
_base_tax_benefit_system = None
39-
_parameters_at_instant_cache = None
42+
_parameters_at_instant_cache: Optional[Dict[Any, Any]] = None
4043
person_key_plural = None
4144
preprocess_parameters = None
4245
baseline = None # Baseline tax-benefit system. Used only by reforms. Note: Reforms can be chained.
4346
cache_blacklist = None
4447
decomposition_file_path = None
4548

46-
def __init__(self, entities):
49+
def __init__(self, entities: Sequence[Entity]) -> None:
4750
# TODO: Currently: Don't use a weakref, because they are cleared by Paste (at least) at each call.
48-
self.parameters = None
51+
self.parameters: Optional[ParameterNode] = None
4952
self._parameters_at_instant_cache = {} # weakref.WeakValueDictionary()
50-
self.variables = {}
51-
self.open_api_config = {}
53+
self.variables: Dict[Any, Any] = {}
54+
self.open_api_config: Dict[Any, Any] = {}
5255
# Tax benefit systems are mutable, so entities (which need to know about our variables) can't be shared among them
5356
if entities is None or len(entities) == 0:
5457
raise Exception("A tax and benefit sytem must have at least an entity.")
@@ -139,13 +142,15 @@ def load_variable(self, variable_class, update = False):
139142

140143
return variable
141144

142-
def add_variable(self, variable):
143-
"""
144-
Adds an OpenFisca variable to the tax and benefit system.
145+
def add_variable(self, variable: Variable) -> Variable:
146+
"""Adds an OpenFisca variable to the tax and benefit system.
147+
148+
Args:
149+
variable: The variable to add. Must be a subclass of Variable.
145150
146-
:param .Variable variable: The variable to add. Must be a subclass of Variable.
151+
Raises:
152+
openfisca_core.errors.VariableNameConflictError: if a variable with the same name have previously been added to the tax and benefit system.
147153
148-
:raises: :exc:`.VariableNameConflictError` if a variable with the same name have previously been added to the tax and benefit system.
149154
"""
150155
return self.load_variable(variable, update = False)
151156

@@ -231,7 +236,7 @@ def load_extension(self, extension):
231236
"""
232237
Loads an extension to the tax and benefit system.
233238
234-
:param string extension: The extension to load. Can be an absolute path pointing to an extension directory, or the name of an OpenFisca extension installed as a pip package.
239+
:param str extension: The extension to load. Can be an absolute path pointing to an extension directory, or the name of an OpenFisca extension installed as a pip package.
235240
236241
"""
237242
# Load extension from installed pip package
@@ -251,19 +256,20 @@ def load_extension(self, extension):
251256
extension_parameters = ParameterNode(directory_path = param_dir)
252257
self.parameters.merge(extension_parameters)
253258

254-
def apply_reform(self, reform_path):
255-
"""
256-
Generates a new tax and benefit system applying a reform to the tax and benefit system.
259+
def apply_reform(self, reform_path: str) -> "TaxBenefitSystem":
260+
"""Generates a new tax and benefit system applying a reform to the tax and benefit system.
257261
258262
The current tax and benefit system is **not** mutated.
259263
260-
:param string reform_path: The reform to apply. Must respect the format *installed_package.sub_module.reform*
264+
Args:
265+
reform_path: The reform to apply. Must respect the format *installed_package.sub_module.reform*
261266
262-
:returns: A reformed tax and benefit system.
267+
Returns:
268+
TaxBenefitSystem: A reformed tax and benefit system.
263269
264270
Example:
265271
266-
>>> self.apply_reform('openfisca_france.reforms.inversion_revenus')
272+
>>> self.apply_reform('openfisca_france.reforms.inversion_revenus')
267273
268274
"""
269275
from openfisca_core.reforms import Reform
@@ -412,9 +418,9 @@ def get_variables(self, entity = None):
412418
"""
413419
Gets all variables contained in a tax and benefit system.
414420
415-
:param .Entity entity: If set, returns only the variable defined for the given entity.
421+
:param Entity entity: If set, returns only the variable defined for the given entity.
416422
417-
:returns: A dictionnary, indexed by variable names.
423+
:returns: A dictionary, indexed by variable names.
418424
:rtype: dict
419425
420426
"""

openfisca_core/tools/test_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def run_tests(tax_benefit_system, paths, options = None):
4444
4545
If `path` is a directory, subdirectories will be recursively explored.
4646
47-
:param .TaxBenefitSystem tax_benefit_system: the tax-benefit system to use to run the tests
47+
:param TaxBenefitSystem tax_benefit_system: the tax-benefit system to use to run the tests
4848
:param str or list paths: A path, or a list of paths, towards the files or directories containing the tests to run. If a path is a directory, subdirectories will be recursively explored.
4949
:param dict options: See more details below.
5050

openfisca_core/variables/variable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def get_formula(self, period = None):
309309
If no period is given and the variable has several formula, return the oldest formula.
310310
311311
:returns: Formula used to compute the variable
312-
:rtype: .Formula
312+
:rtype: :any:`Formula`
313313
314314
"""
315315

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
setup(
4949
name = 'OpenFisca-Core',
50-
version = '35.8.3',
50+
version = '35.8.4',
5151
author = 'OpenFisca Team',
5252
author_email = 'contact@openfisca.org',
5353
classifiers = [

0 commit comments

Comments
 (0)