Skip to content

Commit be35ce9

Browse files
committed
Add documentation about formatting
1 parent 0d0f193 commit be35ce9

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-28
lines changed

doc/faq.rst

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,3 @@ There are several methods to configure your shell:
1818
3. Alternatively, tools like `direnv <https://direnv.net>`_ can be used.
1919

2020
.. _faq_failing_format_check:
21-
22-
Format Still Fails After Running ``project:fix``
23-
------------------------------------------------
24-
25-
If running the following sequence of commands results in ``project:format`` failing with an error during the execution of ``isort``:
26-
27-
#. Run ``project:fix``
28-
#. Run ``project:format``
29-
30-
It is very likely that you did not configure ``isort`` and/or ``black`` appropriately in your ``pyproject.toml`` file.
31-
32-
Ensure ``isort`` is configured with compatibility for ``black``:
33-
34-
.. code-block:: toml
35-
36-
[tool.isort]
37-
profile = "black"
38-
force_grid_wrap = 2
39-
40-
Additionally, your black configuration should look similar to this:
41-
42-
.. code-block:: toml
43-
44-
[tool.black]
45-
line-length = 88
46-
verbose = false
47-
include = "\\.pyi?$"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. _formatting_code:
2+
3+
4+
Formatting code
5+
===============
6+
7+
.. toctree::
8+
:maxdepth: 2
9+
10+
troubleshooting
11+
12+
The PTB allows you to automatically format your code and to ensure via a step in the
13+
``checks.yml`` GitHub workflow that your committed code adheres to these standards. The
14+
goals of this are to improve the readability and maintainability of your code and to
15+
provide a uniform experience across projects.
16+
17+
Nox sessions
18+
++++++++++++
19+
20+
For autoformatting, we use the following tools:
21+
22+
* `black` - automatically formats Python code to maintain consistent styling standards.
23+
* `isort`
24+
* `pyupgrade`
25+
* `ruff` - includes a plethora of tools to automatically format code. In
26+
the PTB, only the following checks are active:
27+
28+
* `unused-import (F401) <https://docs.astral.sh/ruff/rules/unused-import/>`__ - remove unused imports
29+
30+
In the PTB, these tools are bundled into nox sessions to ensure that they are run in a
31+
deterministic manner.
32+
33+
+--------------------+------------------+------------------------------------+
34+
| Nox session | CI Usage | Action |
35+
+====================+==================+====================================+
36+
| ``project:fix`` | No | Applies code formatting changes |
37+
+--------------------+------------------+------------------------------------+
38+
| ``project:format`` | ``checks.yml`` | Checks that current code does not |
39+
| | | need to be re-formatted |
40+
+--------------------+------------------+------------------------------------+
41+
42+
`pre-commit`
43+
++++++++++++
44+
45+
46+
Configuration
47+
+++++++++++++
48+
49+
50+
Ensure ``isort`` is configured with compatibility for ``black``:
51+
52+
.. code-block:: toml
53+
54+
[tool.isort]
55+
profile = "black"
56+
force_grid_wrap = 2
57+
58+
Additionally, your black configuration should look similar to this:
59+
60+
.. code-block:: toml
61+
62+
[tool.black]
63+
line-length = 88
64+
verbose = false
65+
include = "\\.pyi?$"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Troubleshooting
2+
===============
3+
4+
5+
Formatting still fails after running ``project:fix``
6+
----------------------------------------------------
7+
8+
If when you execute:
9+
10+
#. Run ``project:fix``
11+
#. Run ``project:format``
12+
13+
you receive an error from ``project:format`` (i.e. ``isort`` or ``black``), it it
14+
likely that you need to update your configuration to align with :ref:formatting_code.
15+
16+
The automatic formatting is doing x, but we shouldn't do that because of y
17+
---------------------------------------------------------------------------
18+
Usually, automatic formatting is helpful, but there are rare cases where a develop might
19+
in specific areas to ignore automatically applied formatting.
20+
21+
.. note::
22+
To ensure that automatic formatting remains useful, developers should always seek
23+
to use the minimum fix reasonable for the affected code. In most cases, this would
24+
mean adding a comment for a single line.
25+
26+
+-----------------------+--------------------------------+-----------------------+
27+
| Undesired Action | Single line | Within a file |
28+
+=======================+================================+=======================+
29+
| formatting from black | .. code-block:: python |
30+
| | |
31+
| | # fmt: off |
32+
| | <code being ignored by black> |
33+
| | # fmt: on |
34+
+-----------------------+--------------------------------+-----------------------+
35+
| formatting from isort | .. code-block:: python | .. code-block:: python|
36+
| | | |
37+
| | <line-to-ignore> # isort:skip| # isort:skip_file |
38+
+-----------------------+--------------------------------+-----------------------+
39+
| remove unused imports | .. code-block:: python | .. code-block:: python|
40+
| | | |
41+
| | <line-to-ignore> # noqa: F401| # ruff: noqa F401 |
42+
+-----------------------+--------------------------------+-----------------------+
43+
44+
45+
In the ``checks.yml``, ``project:format`` wants me to reformat code I did not modify
46+
------------------------------------------------------------------------------------
47+
48+
This is likely due to one of our tools (i.e. `black`) being upgraded. Within the
49+
`pyproject.toml` of the PTB, dependencies are specified to allow
50+
compatible versions or a restricted version range (i.e., `^6.0.1`, `>=24.1.0,<26.0.0`).
51+
Such specifications should restrict major reformatting changes to coincide only with a
52+
new major version of the PTB. However, sometimes a tool's versioning may not properly
53+
adhere to semantic versioning.
54+
55+
If you encounter this scenario, please:
56+
57+
#. Ensure that your `pyproject.toml` has the PTB restricted to compatible versions
58+
(i.e., `^1.7.0`).
59+
#. Identify which tool is trying to reformat files that you did not modify.
60+
#. Reset your `poetry.lock` to align with your **default branch**.
61+
#. More selectively update your `poetry.lock` with `poetry update <package-name>`.
62+
#. Share with your team which tool & version led to the unexpected changes. So that other
63+
PTB users do not experience the same difficulties, we will update the PTB with a patch
64+
version to avoid this tool's version and later do a major release to better indicate the
65+
breaking changes. You could later create an issue in your GitHub repository to update to
66+
the new major version of the PTB & do the reformatting.

doc/user_guide/features/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
metrics/collecting_metrics
1010
creating_a_release
1111
documentation/index
12+
formatting_code/index
1213
managing_dependencies
1314

1415
Uniform Project Layout

exasol/toolbox/nox/_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ def fix(session: Session) -> None:
4747

4848
@nox.session(name="project:format", python=False)
4949
def fmt_check(session: Session) -> None:
50-
"""Checks the project for correct formatting"""
50+
"""Checks the project for correct formatting_code"""
5151
py_files = python_files(PROJECT_CONFIG.root)
5252
_code_format(session=session, mode=Mode.Check, files=py_files)

0 commit comments

Comments
 (0)