Formatting code

The PTB automatically formats code and ensures via a step in the checks.yml GitHub workflow that committed code adheres to these standards. The goals of this are to improve the readability and maintainability of the code and to provide a uniform experience across projects.

Nox sessions

For autoformatting, the following tools are used:

  • black - formats Python code to maintain consistent styling standards.

  • isort - organizes and formats Python import statements alphabetically and by type (from __future__, standard library packages, third party packages, and local application imports).

  • pyupgrade - upgrades syntax for newer versions of the Python language.

  • ruff - includes a plethora of tools to check and automatically format code. In the PTB, only the following checks are active:

In the PTB, these tools are bundled into nox sessions to ensure that they are run in a deterministic manner.

Nox session

CI Usage

Action

project:fix

No

Applies code formatting changes

project:format

checks.yml

Checks that current code does not need to be re-formatted

Configuration

black

Your black configuration should look similar to this:

[tool.black]
line-length = 88
verbose = false
include = "\\.pyi?$"

For further configuration options, see black configuration.

isort

Ensure isort is configured with compatibility for black:

[tool.isort]
profile = "black"
force_grid_wrap = 2

For further configuration options, see isort options.

pyupgrade

No initial configuration of pyupgrade is required. By default, this is configured to be derived from the minimum Python version that your project supports and is defined in the exasol.toolbox.config.BaseConfig.pyupgrade_argument().

For further configuration options, see the pyupgrade documentation.

ruff

Ensure ruff is configured like so:

[tool.ruff.lint]
extend-ignore = [
    "E",      # Syntax errors
    "F",      # Pyflakes rules (excluding F401)
    "UP",     # pyupgrade rules
    "D",      # Docstring rules
]
extend-select = ["F401"]
unfixable = []

For further configuration options, see ruff options.