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.

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.

For individual configuration, see the pyupgrade CLI options. These can be passed to the Nox sessions via the pyupgrade_args attribute of the noxconfig.Config.