Skip to content

Commit

Permalink
build: normalize pyproject (#156)
Browse files Browse the repository at this point in the history
* Minor change.
* Impacted areas: `**/*`
* Details:
  - Normalize `pyproject.toml`
  - Update OpenFisca-Core to 43.0.0
  • Loading branch information
bonjourmauko authored Oct 16, 2024
2 parents db249b7 + 17f94ad commit 3b3a980
Show file tree
Hide file tree
Showing 22 changed files with 318 additions and 303 deletions.
20 changes: 0 additions & 20 deletions .flake8

This file was deleted.

32 changes: 0 additions & 32 deletions .github/lint-files.sh

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ jobs:

- run: make check-syntax-errors

- run: make check-style

- name: Lint Python files
run: "${GITHUB_WORKSPACE}/.github/lint-files.sh '*.py' 'flake8'"
run: make check-style

- name: Lint YAML tests
run: "${GITHUB_WORKSPACE}/.github/lint-files.sh 'tests/*.yaml' 'yamllint'"
run: make check-yaml

test-yaml:
runs-on: ubuntu-22.04
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

### 7.1.6 [#156](https://github.com/openfisca/country-template/pull/156)

* Minor change.
* Impacted areas: `**/*`
* Details:
- Normalize `pyproject.toml`
- Update OpenFisca-Core to 43.0.0

### 7.1.5 [#154](https://github.com/openfisca/country-template/pull/154)

* Minor change.
Expand Down
16 changes: 11 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ install: deps
@# Install OpenFisca-Extension-Template for development.
@# `make install` installs the editable version of openfisca-country_template.
@# This allows contributors to test as they code.
pip install --editable .[dev] --upgrade --use-deprecated=legacy-resolver
pip install --editable .[dev] --upgrade

build: clean deps
@# Install OpenFisca-Extension-Template for deployment and publishing.
@# `make build` allows us to be be sure tests are run against the packaged version
@# of OpenFisca-Extension-Template, the same we put in the hands of users and reusers.
python -m build
pip uninstall --yes openfisca-country-template
find dist -name "*.whl" -exec pip install --force-reinstall {}[dev] \;

check-syntax-errors:
Expand All @@ -29,15 +30,20 @@ check-syntax-errors:
format-style:
@# Do not analyse .gitignored files.
@# `make` needs `$$` to output `$`. Ref: http://stackoverflow.com/questions/2382764.
ruff format `git ls-files | grep "\.py$$"`
isort `git ls-files | grep "\.py$$"`
autopep8 `git ls-files | grep "\.py$$"`
pyupgrade --py39-plus `git ls-files | grep "\.py$$"`
black `git ls-files | grep "\.py$$"`

check-style:
@# Do not analyse .gitignored files.
@# `make` needs `$$` to output `$`. Ref: http://stackoverflow.com/questions/2382764.
flake8 `git ls-files | grep "\.py$$"`
pylint `git ls-files | grep "\.py$$"`
ruff check `git ls-files | grep "\.py$$"`
isort --check `git ls-files | grep "\.py$$"`
black --check `git ls-files | grep "\.py$$"`

check-yaml:
@# Do not analyse .gitignored files.
@# `make` needs `$$` to output `$`. Ref: http://stackoverflow.com/questions/2382764.
yamllint `git ls-files | grep "\.yaml$$"`

test: clean check-syntax-errors check-style
Expand Down
24 changes: 15 additions & 9 deletions openfisca_country_template/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
This file defines our country's tax and benefit system.
"""This file defines our country's tax and benefit system.
A tax and benefit system is the higher-level instance in OpenFisca.
Its goal is to model the legislation of a country.
Basically a tax and benefit system contains simulation variables (source code) and legislation parameters (data).
Basically a tax and benefit system contains simulation variables (source code)
and legislation parameters (data).
See https://openfisca.org/doc/key-concepts/tax_and_benefit_system.html
"""
Expand All @@ -15,27 +17,31 @@
from openfisca_country_template import entities
from openfisca_country_template.situation_examples import couple


COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__))


# Our country tax and benefit class inherits from the general TaxBenefitSystem class.
# The name CountryTaxBenefitSystem must not be changed, as all tools of the OpenFisca ecosystem expect a CountryTaxBenefitSystem class to be exposed in the __init__ module of a country package.
# Our country tax and benefit class inherits from the general TaxBenefitSystem
# class. The name CountryTaxBenefitSystem must not be changed, as all tools of
# the OpenFisca ecosystem expect a CountryTaxBenefitSystem class to be exposed
# in the __init__ module of a country package.
class CountryTaxBenefitSystem(TaxBenefitSystem):
def __init__(self):
"""Initialize our country's tax and benefit system."""
# We initialize our tax and benefit system with the general constructor
super().__init__(entities.entities)

# We add to our tax and benefit system all the variables
self.add_variables_from_directory(os.path.join(COUNTRY_DIR, "variables"))

# We add to our tax and benefit system all the legislation parameters defined in the parameters files
# We add to our tax and benefit system all the legislation parameters
# defined in the parameters files
param_path = os.path.join(COUNTRY_DIR, "parameters")
self.load_parameters(param_path)

# We define which variable, parameter and simulation example will be used in the OpenAPI specification
# We define which variable, parameter and simulation example will be
# used in the OpenAPI specification
self.open_api_config = {
"variable_example": "disposable_income",
"parameter_example": "taxes.income_tax_rate",
"simulation_example": couple,
}
}
63 changes: 37 additions & 26 deletions openfisca_country_template/entities.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,75 @@
"""
This file defines the entities needed by our legislation.
"""This file defines the entities needed by our legislation.
Taxes and benefits can be calculated for different entities: persons, household, companies, etc.
Taxes and benefits can be calculated for different entities: persons, household,
companies, etc.
See https://openfisca.org/doc/key-concepts/person,_entities,_role.html
"""

from openfisca_core.entities import build_entity

Household = build_entity(
key = "household",
plural = "households",
label = "All the people in a family or group who live together in the same place.",
doc = """
key="household",
plural="households",
label="All the people in a family or group who live together in the same place.",
doc="""
Household is an example of a group entity.
A group entity contains one or more individual·s.
Each individual in a group entity has a role (e.g. parent or children). Some roles can only be held by a limited number of individuals (e.g. a 'first_parent' can only be held by one individual), while others can have an unlimited number of individuals (e.g. 'children').
Each individual in a group entity has a role (e.g. parent or children).
Some roles can only be held by a limited number of individuals (e.g. a
'first_parent' can only be held by one individual), while others can
have an unlimited number of individuals (e.g. 'children').
Example:
Housing variables (e.g. housing_tax') are usually defined for a group entity such as 'Household'.
Housing variables (e.g. housing_tax') are usually defined for a group
entity such as 'Household'.
Usage:
Check the number of individuals of a specific role (e.g. check if there is a 'second_parent' with household.nb_persons(Household.SECOND_PARENT)).
Calculate a variable applied to each individual of the group entity (e.g. calculate the 'salary' of each member of the 'Household' with salaries = household.members("salary", period = MONTH); sum_salaries = household.sum(salaries)).
Check the number of individuals of a specific role (e.g. check if there
is a 'second_parent' with household.nb_persons(Household.SECOND_PARENT)).
Calculate a variable applied to each individual of the group entity
(e.g. calculate the 'salary' of each member of the 'Household' with:
salaries = household.members("salary", period = MONTH)
sum_salaries = household.sum(salaries)).
For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html
""",
roles = [
roles=[
{
"key": "parent",
"plural": "parents",
"label": "Parents",
"max": 2,
"subroles": ["first_parent", "second_parent"],
"doc": "The one or two adults in charge of the household.",
},
},
{
"key": "child",
"plural": "children",
"label": "Child",
"doc": "Other individuals living in the household.",
},
],
)
},
],
)

Person = build_entity(
key = "person",
plural = "persons",
label = "An individual. The minimal legal entity on which a legislation might be applied.",
doc = """
Variables like 'salary' and 'income_tax' are usually defined for the entity 'Person'.
key="person",
plural="persons",
label="An individual. The minimal entity on which legislation can be applied.",
doc="""
Variables like 'salary' and 'income_tax' are usually defined for the entity
'Person'.
Usage:
Calculate a variable applied to a 'Person' (e.g. access the 'salary' of a specific month with person("salary", "2017-05")).
Check the role of a 'Person' in a group entity (e.g. check if a the 'Person' is a 'first_parent' in a 'Household' entity with person.has_role(Household.FIRST_PARENT)).
Calculate a variable applied to a 'Person' (e.g. access the 'salary' of
a specific month with person("salary", "2017-05")).
Check the role of a 'Person' in a group entity (e.g. check if a the
'Person' is a 'first_parent' in a 'Household' entity with
person.has_role(Household.FIRST_PARENT)).
For more information, see: https://openfisca.org/doc/coding-the-legislation/50_entities.html
""",
is_person = True,
)
is_person=True,
)

entities = [Household, Person]
6 changes: 3 additions & 3 deletions openfisca_country_template/reforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This sub-package is used to define reforms.
"""This sub-package is used to define reforms.
A reform is a set of modifications to be applied to a reference tax and benefit system to carry out experiments.
A reform is a set of modifications to be applied to a reference tax and benefit
system to carry out experiments.
See https://openfisca.org/doc/key-concepts/reforms.html
"""
49 changes: 25 additions & 24 deletions openfisca_country_template/reforms/add_dynamic_variable.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
This file defines a reform to add a dynamic variable, based on input data.
"""This file defines a reform to add a dynamic variable, based on input data.
A reform is a set of modifications to be applied to a reference tax and benefit system to carry out experiments.
A reform is a set of modifications to be applied to a reference tax and benefit
system to carry out experiments.
See https://openfisca.org/doc/key-concepts/reforms.html
"""

# Import from openfisca-core the Python objects used to code the legislation in OpenFisca
# Import from openfisca-core the objects used to code the legislation in OpenFisca
from openfisca_core.periods import MONTH
from openfisca_core.reforms import Reform
from openfisca_core.variables import Variable
Expand All @@ -17,36 +17,37 @@

def create_dynamic_variable(name, **variable):
"""Create new variable dynamically."""
NewVariable = type(name, (Variable,), {
"value_type": variable["value_type"],
"entity": variable["entity"],
"default_value": variable["default_value"],
"definition_period": variable["definition_period"],
"label": variable["label"],
"reference": variable["reference"],
})

return NewVariable
return type(
name,
(Variable,),
{
"value_type": variable["value_type"],
"entity": variable["entity"],
"default_value": variable["default_value"],
"definition_period": variable["definition_period"],
"label": variable["label"],
"reference": variable["reference"],
},
)


class add_dynamic_variable(Reform):
def apply(self):
"""
Apply reform.
"""Apply reform.
A reform always defines an `apply` method that builds the reformed tax
and benefit system from the reference one.
See https://openfisca.org/doc/coding-the-legislation/reforms.html#writing-a-reform
"""
NewVariable = create_dynamic_variable(
name = "goes_to_school",
value_type = bool,
entity = Person,
default_value = True,
definition_period = MONTH,
label = "The person goes to school (only relevant for children)",
reference = "https://law.gov.example/goes_to_school",
)
name="goes_to_school",
value_type=bool,
entity=Person,
default_value=True,
definition_period=MONTH,
label="The person goes to school (only relevant for children)",
reference="https://law.gov.example/goes_to_school",
)

self.add_variable(NewVariable)
Loading

0 comments on commit 3b3a980

Please sign in to comment.