Skip to content

Commit fbaaf6c

Browse files
authored
Documentation/555 formatting documentation (#556)
* Add documentation about formatting * Switch references in getting_started to cookiecutter-template * Remove changing output of nox -l as users can just use * Remove unused and likely out of place reference * Clean up from self-review * Move pre-commit information to formatting * Fix broken link * Update dependencies to latest versions * Add sphinx-toolbox as a dependency for collapsible options * Move pre-commit to more general section * Remove description about PYTHON_PATH setting as systemwide concern and fix some typos
1 parent 378e5f5 commit fbaaf6c

File tree

13 files changed

+809
-325
lines changed

13 files changed

+809
-325
lines changed

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"sphinx.ext.napoleon",
3434
"sphinx.ext.intersphinx",
3535
"sphinx.ext.autosummary",
36+
"sphinx_toolbox.collapse",
3637
"sphinx_copybutton",
3738
"myst_parser",
3839
"sphinx_design",

doc/faq.rst

Lines changed: 0 additions & 47 deletions
This file was deleted.

doc/index.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ Documentation of the Exasol-Toolbox
3737

3838
Document outlining the architectural and design principles and decisions in this project.
3939

40-
.. grid-item-card:: :octicon:`question` FAQ
41-
:link: faq_toolbox
42-
:link-type: ref
43-
44-
Frequently asked questions.
45-
4640

4741
.. toctree::
4842
:maxdepth: 4
@@ -52,5 +46,4 @@ Documentation of the Exasol-Toolbox
5246
developer_guide/developer_guide
5347
tools
5448
github_actions/github_actions
55-
faq
5649
changes/changelog
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _formatting_code:
2+
3+
Formatting code
4+
===============
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
troubleshooting
10+
11+
The PTB automatically formats code and ensures via a step in the ``checks.yml`` GitHub
12+
workflow that committed code adheres to these standards. The goals of this are to
13+
improve the readability and maintainability of the code and to provide a uniform
14+
experience across projects.
15+
16+
.. _formatting_sessions:
17+
18+
Nox sessions
19+
++++++++++++
20+
21+
For autoformatting, the following tools are used:
22+
23+
* `black <https://black.readthedocs.io/en/stable/the_black_code_style/index.html>`__ -
24+
formats Python code to maintain consistent styling standards.
25+
* `isort <https://pycqa.github.io/isort/index.html>`__ - organizes and formats Python
26+
import statements alphabetically and by type (from __future__, standard library
27+
packages, third party packages, and local application imports).
28+
* `pyupgrade <https://pypi.org/project/pyupgrade/>`__ - upgrades syntax for newer
29+
versions of the Python language.
30+
31+
In the PTB, these tools are bundled into nox sessions to ensure that they are run in a
32+
deterministic manner.
33+
34+
+--------------------+------------------+------------------------------------+
35+
| Nox session | CI Usage | Action |
36+
+====================+==================+====================================+
37+
| ``project:fix`` | No | Applies code formatting changes |
38+
+--------------------+------------------+------------------------------------+
39+
| ``project:format`` | ``checks.yml`` | Checks that current code does not |
40+
| | | need to be re-formatted |
41+
+--------------------+------------------+------------------------------------+
42+
43+
.. _formatting_configuration:
44+
45+
Configuration
46+
+++++++++++++
47+
black
48+
^^^^^
49+
50+
Your ``black`` configuration should look similar to this:
51+
52+
.. literalinclude:: ../../../../project-template/{{cookiecutter.repo_name}}/pyproject.toml
53+
:language: toml
54+
:start-at: [tool.black]
55+
:end-before: [tool.isort]
56+
57+
For further configuration options, see
58+
`black configuration <https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-format>`__.
59+
60+
isort
61+
^^^^^
62+
Ensure ``isort`` is configured with compatibility for ``black``:
63+
64+
.. literalinclude:: ../../../../project-template/{{cookiecutter.repo_name}}/pyproject.toml
65+
:language: toml
66+
:start-at: [tool.isort]
67+
:end-before: [tool.pylint.master]
68+
69+
For further configuration options, see
70+
`isort options <https://pycqa.github.io/isort/docs/configuration/options.html>`__.
71+
72+
73+
pyupgrade
74+
^^^^^^^^^
75+
No initial configuration of ``pyupgrade`` is required.
76+
77+
For individual configuration, see the
78+
`pyupgrade CLI options <https://pypi.org/project/pyupgrade/>`__. These can
79+
be passed to the :ref:`formatting_sessions` via the ``pyupgrade_args``
80+
attribute of the :class:`noxconfig.Config`.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
.. _formatting_troubleshooting:
2+
3+
Troubleshooting
4+
===============
5+
6+
Formatting still fails after running ``project:fix``
7+
----------------------------------------------------
8+
9+
If when you execute:
10+
11+
#. Run ``project:fix``
12+
#. Run ``project:format``
13+
14+
you receive an error from ``project:format`` (i.e. ``isort`` or ``black``), it it
15+
likely that you need to update your configuration to align with
16+
:ref:`formatting_configuration`.
17+
18+
The automatic formatting is doing x, but we shouldn't do that because of y
19+
---------------------------------------------------------------------------
20+
Usually, automatic formatting is helpful, but there are rare cases where a developer
21+
might want to ignore automatically applied formatting.
22+
23+
.. note::
24+
To ensure that automatic formatting remains useful, developers should always seek
25+
to use the minimum fix reasonable for the affected code. In most cases, this would
26+
mean adding a comment for a single line.
27+
28+
+-----------------------+--------------------------------+-----------------------+
29+
| Undesired Action | Single line | Within a file |
30+
+=======================+================================+=======================+
31+
| formatting from black | .. code-block:: python |
32+
| | |
33+
| | # fmt: off |
34+
| | <code being ignored by black> |
35+
| | # fmt: on |
36+
+-----------------------+--------------------------------+-----------------------+
37+
| formatting from isort | .. code-block:: python | .. code-block:: python|
38+
| | | |
39+
| | <line-to-ignore> # isort:skip| # isort:skip_file |
40+
+-----------------------+--------------------------------+-----------------------+
41+
42+
43+
In the ``checks.yml``, ``project:format`` wants me to reformat code I did not modify
44+
------------------------------------------------------------------------------------
45+
46+
This is likely due to one of our tools (i.e. ``black``) being upgraded. Within the
47+
``pyproject.toml`` of the PTB, dependencies are specified to allow
48+
compatible versions or a restricted version range (i.e., ``^6.0.1``, ``>=24.1.0,<26.0.0``).
49+
Such specifications should restrict major reformatting changes to coincide only with a
50+
new major version of the PTB. However, sometimes a tool's versioning may not properly
51+
adhere to semantic versioning.
52+
53+
If you encounter this scenario, please:
54+
55+
#. Ensure that your ``pyproject.toml`` has the PTB restricted to compatible versions
56+
(i.e., ``^1.7.0``).
57+
#. Identify which tool is trying to reformat files that you did not modify.
58+
#. Reset your ``poetry.lock`` to align with what's in the project's **default branch**.
59+
#. More selectively update your ``poetry.lock`` with `poetry update <package-name>`.
60+
#. Share with your team which tool & version led to the unexpected changes. So that
61+
other PTB users do not experience the same difficulties, we will update the PTB with
62+
a patch version to avoid this tool's version and later do a major release to better
63+
indicate the breaking changes. You could later create an issue in your GitHub
64+
repository to update to the new major version of the PTB & do the reformatting.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. _git_hooks:
2+
3+
Enabling Git Hooks
4+
==================
5+
6+
.. toctree::
7+
:maxdepth: 2
8+
9+
troubleshooting
10+
11+
Git hooks are automated scripts that run at specific points during Git's execution,
12+
allowing developers to enforce quality standards, automate tasks, and customize Git's
13+
behavior. The PTB uses the `pre-commit <https://pre-commit.com/>`__ framework to
14+
define common Git hooks for Git commits and pushes. The configuration for
15+
``pre-commit`` is provided in a ``.pre-commit-config.yaml`` file and described in
16+
:ref:`pre-commit_configuration`.
17+
18+
.. _pre-commit_configuration:
19+
20+
Configuration
21+
+++++++++++++
22+
23+
#. Add a ``.pre-commit-config.yaml`` file to your project's root directory. Feel free to
24+
take inspiration from the example ``.pre-commit-config.yaml``:
25+
26+
.. collapse:: .pre-commit-config.yaml
27+
28+
.. literalinclude:: ../../../../project-template/{{cookiecutter.repo_name}}/.pre-commit-config.yaml
29+
:language: yaml
30+
31+
32+
#. Enable pre-commit hooks for your workspace:
33+
34+
.. code-block:: shell
35+
36+
poetry run -- pre-commit install --hook-type pre-commit --hook-type pre-push
37+
38+
39+
Working with ``pre-commit``
40+
+++++++++++++++++++++++++++
41+
42+
.. _committing:
43+
44+
Committing
45+
----------
46+
47+
Once ``pre-commit`` has been configured, the process for performing a ``git commit`` is:
48+
49+
#. Make your code changes
50+
#. ``git add`` changed files and ``git commit -m "<message>"``
51+
#. ``pre-commit`` performs checks on the changed files and produces an output like
52+
53+
.. code-block:: bash
54+
55+
code-format..........................................(no files to check)Skipped
56+
check yaml...........................................(no files to check)Skipped
57+
fix end of files.........................................................Passed
58+
trim trailing whitespace.................................................Passed
59+
60+
* If all steps pass, then no action is needed.
61+
* If a step fails, then check the output further. If it was an automatic fix, then
62+
just add the altered file to your commit and execute your ``git commit`` line again.
63+
Otherwise, manual intervention is needed.
64+
65+
Pushing
66+
-------
67+
68+
Once ``pre-commit`` has been configured, the process for performing a ``git push`` is:
69+
70+
#. Perform one or more iterations of :ref:`committing`.
71+
#. ``git push``
72+
#. ``pre-commit`` performs checks on the changed files and produces an output like
73+
74+
.. code-block:: bash
75+
76+
type-check...............................................................Passed
77+
lint.....................................................................Passed
78+
79+
* If all steps pass, then no action is needed.
80+
* If a step fails, then check the output further. The suggested ``pre-push`` actions
81+
given in the :ref:`pre-commit_configuration` require manual intervention. Create
82+
a new commit to resolve the issue & try ``git push`` again.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Troubleshooting
2+
===============
3+
4+
The checks for ``git commit`` or ``git push`` keep failing
5+
----------------------------------------------------------
6+
7+
The ``pre-commit`` hooks, as defined in the ``.pre-commit-config.yaml``
8+
use multiple nox sessions. If a step fails multiple times, despite a user adding fixes
9+
or manually resolving the error, then please check which nox session is failing
10+
and refer to its troubleshooting documentation for help:
11+
12+
* :ref:`Formatting Troubleshooting <formatting_troubleshooting>`

doc/user_guide/features/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Features
99
metrics/collecting_metrics
1010
creating_a_release
1111
documentation/index
12+
git_hooks/index
13+
formatting_code/index
1214
managing_dependencies
1315

1416
Uniform Project Layout

0 commit comments

Comments
 (0)