-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add editorial content on parameter and variable #717
Conversation
@sandcha : I am ok with that. My only concern is performance. Does the inclusion of an new attribute for all the variables may alter performance when instantiating a tax benefit system ? |
tests/web_api/test_variables.py
Outdated
response = subject.get('/variable/housing_allowance') | ||
variable = json.loads(response.data.decode('utf-8')) | ||
assert_equal(variable['documentation'], | ||
"\nThis allowance was introduced on the 1st of Jan 1980.\nIt needs the 'rent' value " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first line break is not relevant, and should be removed somehow. Same for the last one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #714 (comment) for an easy implem
@@ -78,6 +78,9 @@ def build_variable(variable, country_package_metadata, tax_benefit_system): | |||
), | |||
} | |||
|
|||
if variable.documentation: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at OpenFisca France, a lot of doc string is added at the formula level. This PR is not exposing this doc though the API. We should probably do that too :)
@@ -120,19 +120,23 @@ def __init__(self, name, data, file_path = None): | |||
_validate_parameter(self, data, data_type = dict) | |||
self.description = None | |||
self.metadata = {} | |||
self.documentation = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add this new attributes to parameter nodes? I'm not sure there is a good reason to do it only for leads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you see some use case for documentation on ParameterNode
objects?
As I see it, for now, we wouldn't use it so it would be over-engineering it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, imagine something like: rsa.plafonds.1_enfant
, rsa.plafonds.2_enfants
, rsa.plafonds.par_enfant_supp
.
In that case, it's probably more interesting to document the rsa.plafonds
rather than the specific values.
In many case, parameters work by group and are not semantically fully independent. In that case, the nodes may be the thing we want to document.
tests/core/test_parameters.py
Outdated
'A fraction of the rent. \nFrom the 1st of Dec 2016, the housing allowance no longer exists.\n') | ||
|
||
|
||
@raises(AttributeError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if we chose not to give parameter nodes a documentation, I'm not sure about the relevance of such a test.
When reading this test, it feels like we are testing that Python sends an
AttributeError
when calling an undefined attribute, which doesn't say much about OpenFisca itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
It was there to define the expected behaviour before coding but is irrelevant now.
openfisca_core/parameters.py
Outdated
:param string name: Name of the parameter, e.g. "taxes.some_tax.some_param" | ||
:param dict data: Data loaded from a YAML file. | ||
:param string file_path: File the parameter was loaded from. | ||
:param string documentation: Some documentation describing parameter usage and context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some documentation -> Documentation
openfisca_core/parameters.py
Outdated
@@ -315,6 +320,7 @@ def __init__(self, name = "", directory_path = None, data = None, file_path = No | |||
:param string name: Name of the node, eg "taxes.some_tax". | |||
:param string directory_path: Directory containing YAML files describing the node. | |||
:param dict data: Object representing the parameter node. It usually has been extracted from a YAML file. | |||
:param string documentation: Some documentation describing parameter node usage and context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some documentation -> Documentation
openfisca_core/parameters.py
Outdated
self.description = data.get('description', None) | ||
self.documentation = data.get('documentation', None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None
is already the default 2nd argument of get, specifying is redundant.
openfisca_core/parameters.py
Outdated
self.description = data.get('description', None) | ||
self.documentation = data.get('documentation', None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary 2nd argument
@@ -378,8 +386,9 @@ def __init__(self, name = "", directory_path = None, data = None, file_path = No | |||
else: | |||
self.file_path = file_path | |||
_validate_parameter(self, data, data_type = dict, allowed_keys = self._allowed_keys) | |||
# We allow to set a reference and a description for a node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment seems outdated (we don't set a reference around it). I'd just remove it 🙂
@@ -78,6 +82,9 @@ def build_variable(variable, country_package_metadata, tax_benefit_system): | |||
), | |||
} | |||
|
|||
if variable.documentation: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way we handle unspecified documentation in the Web API is currently inconsistent:
documentation: null for parameters
documentation: "" for formulas
- no
documentation
key for variables
I'd be in favor of generalizing the 3rd option, as it makes the JSON more readable, lighter, and doesn't change anything for JS clients.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed for Web API : a documentation attribute is exposed only when it exists and its value is cleaned from start and end spaces.
On Python API : the documentation attribute value is not modified at all.
For variable formulas, there is no specificdocumentation
attribute as the user can already get formula docstring withformula.__doc__
.
tests/core/test_parameters.py
Outdated
def test_parameter_documentation(): | ||
parameter = tax_benefit_system.parameters.benefits.housing_allowance | ||
assert_equal(parameter.documentation, | ||
'A fraction of the rent. \nFrom the 1st of Dec 2016, the housing allowance no longer exists.\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This last line break is not really relevant. Cleaning the trailing line breaks with a
strip
in parameters parsing would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done for web API (not for python API).
tests/web_api/test_variables.py
Outdated
variable = json.loads(response.data.decode('utf-8')) | ||
assert_equal(variable['documentation'], | ||
"This allowance was introduced on the 1st of Jan 1980.\nIt needs the 'rent' value " | ||
+ "(same month) but doesn't care about the 'housing_occupancy_status'.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The +
is not necessary, Python automatically concatenates strings.
@@ -68,7 +68,11 @@ def test_parameter_node(): | |||
assert_equal(response.status_code, OK) | |||
parameter = json.loads(response.data) | |||
assert_equal(sorted(list(parameter.keys())), ['description', 'documentation', 'id', 'metadata', 'source', 'subparams']) | |||
assert_equal(parameter['documentation'], 'This (optional) file defines metadata for the node parameters.benefits in the parameter tree.') | |||
assert_equal(parameter['documentation'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For test robustness sake, I'd just check that
parameter['documentation']
contains a key expression, like "Government support for the citizens and residents".
@benjello Including one attribute of primitive type for all the variables won't have significant effect on tax benefit system instantiation. For now, documentation attributes will mainly gather existing comments and have very short contents. |
8fcc772
to
c638fcd
Compare
…I and strip its value
89a4bb8
to
691fbde
Compare
a438a85
to
f194591
Compare
Connected to openfisca/legislation-explorer#124
New features
Allow for multiline editorial content on parameters and variables:
ParameterNode.documentation
,Parameter.documentation
andVariable.documentation
attributesdocumentation
attribute through Web API/parameter
nodes as/parameter/benefits
/parameter
leafs as/parameter/benefits/housing_allowance
/variable
leafs as/variable/housing_allowance
/variable
leaf formulaHere is web API
/variable/housing_allowance
example:The formula doctoring is in the formula
content
anddocumentation
attributes.