Code style

Nox or pre-commit performs automatic formatting and linting of the code.

Lint with nox

After successful setup, Nox sessions can be executed to perform lint checks:

nox -s lint

This runs isort, black, flake8 and codespell and fixes almost all formatting and linting problems automatically, but a small number must be fixed by hand (e.g. unused imports).

Note

To reuse the current virtual environment and avoid lengthy installation processes, use the -R flag:

nox -R -s lint

Note

pre-commit can also be run from nox:

nox -s pre_commit

Lint with pre-commit

Alternatively, use pre-commit to perform the same job. pre-commit hooks need to be installed:

pre-commit install

Now, each use of git commit in the shell will trigger actions defined in the .pre-commit-config.yaml file. pre-commit is easily deactivated with a similar command:

pre-commit uninstall

If you wish to run pre-commit asynchronously, use:

pre-commit run --all-files

Note

pre-commit by default also runs nox with isort, flake8, black and codespell sessions.

Tools

Tools used in the Topwrap project for maintaining the code style:

  • nox is a tool, which simplifies management of Python testing.

  • pre-commit is a framework for managing and maintaining multi-language pre-commit hooks.

  • black is a Python code formatter.

  • flake8 is a tool capable of linting, styling fixes and complexity analysis of Python code.

  • isort is a Python utility to sort imports alphabetically.

  • codespell is a Python tool to fix common spelling mistakes in text files


Last update: 2024-12-10