Skip to content

Commit 93c0f4f

Browse files
committed
Add documentation about formatting
1 parent 08c6536 commit 93c0f4f

File tree

4 files changed

+125
-27
lines changed

4 files changed

+125
-27
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
26+
In the PTB, these tools are bundled into nox sessions to ensure that they are run in a
27+
deterministic manner.
28+
29+
+--------------------+------------------+------------------------------------+
30+
| Nox session | CI Usage | Action |
31+
+====================+==================+====================================+
32+
| ``project:fix`` | No | Applies code formatting changes |
33+
+--------------------+------------------+------------------------------------+
34+
| ``project:format`` | ``checks.yml`` | Checks that current code does not |
35+
| | | need to be re-formatted |
36+
+--------------------+------------------+------------------------------------+
37+
38+
`pre-commit`
39+
++++++++++++
40+
41+
42+
Configuration
43+
+++++++++++++
44+
45+
46+
Ensure ``isort`` is configured with compatibility for ``black``:
47+
48+
.. code-block:: toml
49+
50+
[tool.isort]
51+
profile = "black"
52+
force_grid_wrap = 2
53+
54+
Additionally, your black configuration should look similar to this:
55+
56+
.. code-block:: toml
57+
58+
[tool.black]
59+
line-length = 88
60+
verbose = false
61+
include = "\\.pyi?$"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
# TODO REFERENCE
16+
17+
The automatic formatting is doing x, but we shouldn't do that because of y
18+
---------------------------------------------------------------------------
19+
Usually, automatic formatting is helpful, but there are rare cases where a developer
20+
might want to ignore automatically applied formatting.
21+
22+
.. note::
23+
To ensure that automatic formatting remains useful, developers should always seek
24+
to use the minimum fix reasonable for the affected code. In most cases, this would
25+
mean adding a comment for a single line.
26+
27+
+-----------------------+--------------------------------+-----------------------+
28+
| Undesired Action | Single line | Within a file |
29+
+=======================+================================+=======================+
30+
| formatting from black | .. code-block:: python |
31+
| | |
32+
| | # fmt: off |
33+
| | <code being ignored by black> |
34+
| | # fmt: on |
35+
+-----------------------+--------------------------------+-----------------------+
36+
| formatting from isort | .. code-block:: python | .. code-block:: python|
37+
| | | |
38+
| | <line-to-ignore> # isort:skip| # isort:skip_file |
39+
+-----------------------+--------------------------------+-----------------------+
40+
41+
42+
In the ``checks.yml``, ``project:format`` wants me to reformat code I did not modify
43+
------------------------------------------------------------------------------------
44+
45+
This is likely due to one of our tools (i.e. `black`) being upgraded. Within the
46+
`pyproject.toml` of the PTB, dependencies are specified to allow
47+
compatible versions or a restricted version range (i.e., `^6.0.1`, `>=24.1.0,<26.0.0`).
48+
Such specifications should restrict major reformatting changes to coincide only with a
49+
new major version of the PTB. However, sometimes a tool's versioning may not properly
50+
adhere to semantic versioning.
51+
52+
If you encounter this scenario, please:
53+
54+
#. Ensure that your `pyproject.toml` has the PTB restricted to compatible versions
55+
(i.e., `^1.7.0`).
56+
#. Identify which tool is trying to reformat files that you did not modify.
57+
#. Reset your `poetry.lock` to align with what's in the project's **default branch**.
58+
#. More selectively update your `poetry.lock` with `poetry update <package-name>`.
59+
#. Share with your team which tool & version led to the unexpected changes. So that
60+
other PTB users do not experience the same difficulties, we will update the PTB with
61+
a patch version to avoid this tool's version and later do a major release to better
62+
indicate the breaking changes. You could later create an issue in your GitHub
63+
repository to update to 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

0 commit comments

Comments
 (0)