From 1bd0c626e9b74088a930f90115d1cffc77795f7c Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Tue, 22 Mar 2022 10:40:11 +0000 Subject: [PATCH 1/3] Add failing unit test --- .../tax_scales/test_tax_scale_descendants.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/core/tax_scales/test_tax_scale_descendants.py diff --git a/tests/core/tax_scales/test_tax_scale_descendants.py b/tests/core/tax_scales/test_tax_scale_descendants.py new file mode 100644 index 0000000000..bcddc461f4 --- /dev/null +++ b/tests/core/tax_scales/test_tax_scale_descendants.py @@ -0,0 +1,47 @@ +def test_tax_scale_descendants_are_complete(): + """Tests that calling the `get_descendant` function of a ParameterScale returns the rates and thresholds as Parameters. + """ + + from openfisca_core.parameters import ParameterNode, Parameter + + parameters = ParameterNode("parameters", data={ + "root_node": { + "first_child": { + "scale_parameter": { + "brackets": [ + { + "rate": { + "2022-01-01": 0.1, + }, + "threshold": { + "2022-01-01": 0, + } + }, + { + "rate": { + "2022-01-01": 0.2, + }, + "threshold": { + "2022-01-01": 100, + } + } + ] + }, + "normal_parameter": { + "2022-01-01": 5 + } + } + } + }) + + # Get descendants which are parameters (ignore nodes) + parameter_descendants = list(filter(lambda p: isinstance(p, Parameter), parameters.get_descendants())) + + # Check that the expected names are in the descendants + parameter_names = list(map(lambda p: p.name, parameter_descendants)) + assert all(name in parameter_names for name in [ + "root_node.first_child.scale_parameter[0].rate", + "root_node.first_child.scale_parameter[0].threshold", + "root_node.first_child.scale_parameter[1].rate", + "root_node.first_child.scale_parameter[1].threshold", + ]), "ParameterScale descendants don't include bracket thresholds and rates" From b3a7cbd6afa3453b7626a4c5adcb45e7cc3c7cf8 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Tue, 22 Mar 2022 10:46:58 +0000 Subject: [PATCH 2/3] Implement fix --- openfisca_core/parameters/parameter_scale.py | 9 +++- .../tax_scales/test_tax_scale_descendants.py | 48 +++++++++---------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/openfisca_core/parameters/parameter_scale.py b/openfisca_core/parameters/parameter_scale.py index d1cfc26379..285e3cc85b 100644 --- a/openfisca_core/parameters/parameter_scale.py +++ b/openfisca_core/parameters/parameter_scale.py @@ -62,7 +62,14 @@ def __repr__(self): ) def get_descendants(self): - return iter(()) + returned_any_results = False + for bracket in self.brackets: + for allowed_key_name in bracket._allowed_keys: + if hasattr(bracket, allowed_key_name): + yield getattr(bracket, allowed_key_name) + returned_any_results = True + if not returned_any_results: + return iter(()) def clone(self): clone = commons.empty_clone(self) diff --git a/tests/core/tax_scales/test_tax_scale_descendants.py b/tests/core/tax_scales/test_tax_scale_descendants.py index bcddc461f4..2ae2e25cd7 100644 --- a/tests/core/tax_scales/test_tax_scale_descendants.py +++ b/tests/core/tax_scales/test_tax_scale_descendants.py @@ -9,30 +9,30 @@ def test_tax_scale_descendants_are_complete(): "first_child": { "scale_parameter": { "brackets": [ - { - "rate": { - "2022-01-01": 0.1, - }, - "threshold": { - "2022-01-01": 0, - } + { + "rate": { + "2022-01-01": 0.1, + }, + "threshold": { + "2022-01-01": 0, + } + }, + { + "rate": { + "2022-01-01": 0.2, + }, + "threshold": { + "2022-01-01": 100, + } + } + ] }, - { - "rate": { - "2022-01-01": 0.2, - }, - "threshold": { - "2022-01-01": 100, - } - } - ] - }, "normal_parameter": { "2022-01-01": 5 + } } } - } - }) + }) # Get descendants which are parameters (ignore nodes) parameter_descendants = list(filter(lambda p: isinstance(p, Parameter), parameters.get_descendants())) @@ -40,8 +40,8 @@ def test_tax_scale_descendants_are_complete(): # Check that the expected names are in the descendants parameter_names = list(map(lambda p: p.name, parameter_descendants)) assert all(name in parameter_names for name in [ - "root_node.first_child.scale_parameter[0].rate", - "root_node.first_child.scale_parameter[0].threshold", - "root_node.first_child.scale_parameter[1].rate", - "root_node.first_child.scale_parameter[1].threshold", - ]), "ParameterScale descendants don't include bracket thresholds and rates" + "parameters.root_node.first_child.scale_parameter[0].rate", + "parameters.root_node.first_child.scale_parameter[0].threshold", + "parameters.root_node.first_child.scale_parameter[1].rate", + "parameters.root_node.first_child.scale_parameter[1].threshold", + ]), "ParameterScale descendants don't include bracket thresholds and rates" From 44a045574a070e59e09682b88aa14e872c245d41 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Tue, 22 Mar 2022 10:48:54 +0000 Subject: [PATCH 3/3] Patch bump version and add to changelog --- CHANGELOG.md | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2af91899d7..dea2a9a195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +### 35.7.9 [#1113](https://github.com/openfisca/openfisca-core/pull/1113) + +### Technical changes + +- Fixed a bug where `get_descendants` would not return rates and thresholds from `ParameterScale`s. + ### 35.7.8 [#1086](https://github.com/openfisca/openfisca-core/pull/1086) #### Technical changes diff --git a/setup.py b/setup.py index 98904fe43b..2fc19f626f 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,7 @@ setup( name = 'OpenFisca-Core', - version = '35.7.8', + version = '35.7.9', author = 'OpenFisca Team', author_email = 'contact@openfisca.org', classifiers = [