Skip to content

Commit 7566d96

Browse files
author
Mauko Quiroga
authored
Make CountryTaxBenefitSystem reusable as a fixture
Merge pull request #997 from openfisca/extract-tbs-fixture
2 parents f504420 + 7311cea commit 7566d96

28 files changed

Lines changed: 854 additions & 757 deletions

CHANGELOG.md

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

3+
### 35.3.5 [#997](https://github.com/openfisca/openfisca-core/pull/997)
4+
5+
#### Technical changes
6+
7+
- In tests, extract `CountryTaxBenefitSystem` to a fixture reusable by all the tests in the test suite.
8+
- It allows for a better reusability of test scenarios available to new contributors.
9+
- To mitigate possible performance issues, by default the fixture is initialised once per test module.
10+
311
### 35.3.4 [#999](https://github.com/openfisca/openfisca-core/pull/999)
412

513
#### Technical improvements

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pytest tests/core/test_parameters.py -k test_parameter_for_period
5151

5252
## Style
5353

54-
This repository adheres to a certain coding style, and we invite you to follow it for your contributions to be integrated promptly.
54+
This repository adheres to a [certain coding style](STYLEGUIDE.md), and we invite you to follow it for your contributions to be integrated promptly.
5555

5656
Style checking is already run with `make test`. To run the style checker alone:
5757

STYLEGUIDE.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# OpenFisca's Python Style Guide
2+
3+
Arguments over code style and formatting are the bread and butter of most open-source projets out there, including OpenFisca.
4+
5+
To avoid this, we have a style guide, that is a set or arbitrary but consistent conventions about how code should be written, for contributors and maintainers alike.
6+
7+
## Notice
8+
9+
### About this style guide
10+
11+
The present style guide is a work in progress.
12+
13+
It largely follows [Python Foundation's](https://www.python.org/dev/peps/pep-0008/), [NumPy's](https://numpydoc.readthedocs.io/en/latest/format.html) and [Google's](https://google.github.io/styleguide/pyguide.html), but it has a lot of her own as well.
14+
15+
### Contributing
16+
17+
Please refer whenever possible to this style guide both for your contributions and your reviews.
18+
19+
If the style in question is not present and contentious, do not hesitate to include an addition to this guide within your proposal or review.
20+
21+
## Imports
22+
23+
1. In general, use import statements for entire namespaces and modules, rather than for classes and functions. Are exempt of this rule the `typing` module, NumPy's `typing` module, and `openfisca_core` module and submodules (but only in the case of class imports).
24+
25+
2. In general, use absolute import statements rather that relative ones. Are exempt of this rule the modules relative to a submodule of OpenFisca, in order to improve the delimitation of internal and external interfaces.
26+
27+
3. Always follow this order for your imports: system modules, third party modules, third party OpenFisca modules, external OpenFisca Core modules, internal OpenFisca Core modules.
28+
29+
For example given:
30+
31+
```
32+
/openfisca_core/axes/__init__.py
33+
/openfisca_core/axes/nothing.py
34+
/openfisca_core/axes/something.py
35+
```
36+
37+
Whenever possible we should expect:
38+
39+
```python
40+
# /openfisca_core/axes/nothing.py
41+
#
42+
# Yes
43+
44+
import copy
45+
from typing import List
46+
47+
import numpy
48+
from numpy.typing import ArrayLike
49+
50+
from openfisca_country_template import entities
51+
52+
from openfisca_core import tools
53+
from openfisca_core.variables import Variable
54+
55+
from . import Something
56+
57+
def do(this: List) -> ArrayLike:
58+
that = copy.deepcopy(this)
59+
array = numpy.ndarray(that)
60+
return Something(entities.Person, Variable)
61+
```
62+
63+
And avoid:
64+
65+
```python
66+
# /openfisca_core/axes/nothing.py
67+
#
68+
# No
69+
70+
from openfisca_country_template.entities import Person
71+
from openfisca_core import variables
72+
from openfisca_core.tools import assert_near
73+
from openfisca_core import axes
74+
75+
from numpy import ndarray
76+
from copy import deepcopy
77+
import typing
78+
import numpy.typing
79+
80+
def do(this: typing.List) -> numpy.typing.ArrayLike:
81+
that = deepcopy(this)
82+
array = ndarray(that)
83+
return axes.Something(Person, variables.Variable)
84+
```

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest_plugins = [
2+
"tests.fixtures.entities",
3+
"tests.fixtures.simulations",
4+
"tests.fixtures.taxbenefitsystems",
5+
]

setup.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#! /usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
42

53
from setuptools import setup, find_packages
64

@@ -37,7 +35,7 @@
3735

3836
setup(
3937
name = 'OpenFisca-Core',
40-
version = '35.3.4',
38+
version = '35.3.5',
4139
author = 'OpenFisca Team',
4240
author_email = 'contact@openfisca.org',
4341
classifiers = [

tests/__init__.py

Whitespace-only changes.

tests/core/parameter_validation/test_parameter_clone.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# -*- coding: utf-8 -*-
2-
from ..test_countries import tax_benefit_system
3-
41
import os
2+
53
from openfisca_core.parameters import ParameterNode
4+
65
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
76
year = 2016
87

@@ -20,7 +19,7 @@ def test_clone():
2019
assert id(clone.node1.param) != id(parameters.node1.param)
2120

2221

23-
def test_clone_parameter():
22+
def test_clone_parameter(tax_benefit_system):
2423

2524
param = tax_benefit_system.parameters.taxes.income_tax_rate
2625
clone = param.clone()
@@ -32,7 +31,7 @@ def test_clone_parameter():
3231
assert clone.values_list == param.values_list
3332

3433

35-
def test_clone_parameter_node():
34+
def test_clone_parameter_node(tax_benefit_system):
3635
node = tax_benefit_system.parameters.taxes
3736
clone = node.clone()
3837

@@ -41,15 +40,15 @@ def test_clone_parameter_node():
4140
assert clone.children['income_tax_rate'] is not node.children['income_tax_rate']
4241

4342

44-
def test_clone_scale():
43+
def test_clone_scale(tax_benefit_system):
4544
scale = tax_benefit_system.parameters.taxes.social_security_contribution
4645
clone = scale.clone()
4746

4847
assert clone.brackets[0] is not scale.brackets[0]
4948
assert clone.brackets[0].rate is not scale.brackets[0].rate
5049

5150

52-
def test_deep_edit():
51+
def test_deep_edit(tax_benefit_system):
5352
parameters = tax_benefit_system.parameters
5453
clone = parameters.clone()
5554

0 commit comments

Comments
 (0)