Skip to content

Formatting Rules

The formatting rules rewrite the small details of a statement once its layout is settled, each one changing a token, a line, or a spelling rather than the construct around it. Every rewrite is narrower than a layout rule and more pervasive than an ordering rule, tidying what the eye reads without moving the structure it reads. The rules whose output depends on the runtime read target-version, so a rewrite is written only where the Python version a project ships to accepts it.

modernize-annotations

Formatting

Rewrites Optional[T], Union[X, Y], and the typing generics to the T | None, X | Y, and builtin forms the target runtime supports.

normalize-comment-spacing

Formatting

Puts one space after a comment's hash run and at least two spaces between code and a trailing comment.

normalize-comparisons

Formatting

Rewrites a comparison to state its check directly, using is against None, putting the variable side first, and folding a leading not into its operator.

normalize-literals

Formatting

Rewrites every string and numeric literal to one spelling of quote character, string prefix, and numeric case.

prefer-fstring

Formatting

Converts printf-style % interpolation and str.format() calls to f-strings wherever both forms render the same text.

prune-inert-imports

Formatting

Removes an import that binds a name nothing references or repeats a binding an earlier import already made, and reports the unreferenced one in a package __init__.py instead of removing it.

shed-backslash-continuations

Formatting

Removes a trailing backslash and rejoins its statement, adding parentheses where the joined line would overflow the budget.

shed-redundant-base

Formatting

Removes an explicit object base and the empty parentheses on a class header with no bases.

shed-super-args

Formatting

Removes the class and instance arguments from a super(C, self) call, leaving the bare super() the interpreter resolves on its own.

simplify-comprehensions

Formatting

Removes a collection constructor wrapped around a literal, comprehension, or generator that already builds that collection, and replaces a comprehension that copies its input unchanged with the constructor call.

space-statements

Formatting

Sets the blank-line count between module-level definitions, class members, import groups, and the __main__ guard to PEP 8's canonical values.

strip-none-return

Formatting

Removes a bare -> None return annotation, since an omitted one already reads as returning nothing.

strip-trailing-commas

Formatting

Removes the trailing comma from a collection, signature, call, class base list, or type-parameter list, leaving tuples alone.

For enabling or disabling any of these rules, see the Configuration reference. For the pipeline order they run in, see the Pipeline Order reference.