|
| 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 | +``` |
0 commit comments