1.. _python_style:
2
3Python Style Guidelines
4#######################
5
6Python should be formatted in a `PEP 8`_ compliant manner. Zephyr uses the `ruff formatter`_
7to achieve this. This opinionated formatter aims for consistency, generality, readability and
8reducing git diffs.
9
10To apply the formatter, run:
11
12.. code-block:: shell
13
14   ruff check --select I --fix <file> # Sort imports
15   ruff format <file>
16
17Ruff configuration
18******************
19
20A small set of options is applied on top of the defaults:
21
22* Line length of 100 columns or fewer.
23* Both single ``'`` and double ``"`` quote styles are allowed.
24* Line endings will be converted to ``\n``. The default line ending on Unix.
25
26Excluded files
27**************
28
29The formatter is enforced in CI, but only for newly added Python files, because the project already
30had a large Python codebase when this was introduced.
31The :zephyr_file:`.ruff-excludes.toml` file has a ``[format]`` section where all the files that are
32currently excluded are listed. It is encouraged for contributors, when changing an excluded file,
33to remove it from the list and format it in a separate commit.
34
35.. _PEP 8:
36   https://peps.python.org/pep-0008/
37
38.. _ruff formatter:
39   https://docs.astral.sh/ruff/formatter/
40