Python Code Formatter

Python Code Formatter

black.readthedocs.io

6

About this website

The uncompromising Python code formatter, currently at version 26.5.1, that automatically reformats entire codebases to a consistent style, eliminating debates about formatting conventions in code reviews. Developed under the Python Software Foundation, the tool takes the radical position that any formatted output is correct as long as it is consistent, removing developer agency over minutiae like line length, string quote style, trailing commas, and whitespace around operators. The tool formats one logical line at a time, wrapping long lines intelligently by identifying the innermost data structure or function call and splitting at the most logical boundary. String normalization converts all string literals to double quotes unless the string contains double quotes, with support for mixing quote styles in f-strings and avoiding escapes where possible. Parenthesis handling normalizes trailing parentheses in wrapped function calls, while magic trailing commas signal intentional multi-line formatting that should be preserved. Configuration is minimal, with only a line length setting and target Python version truly affecting output, though options exist for skipping specific patterns, excluding directories, and enabling experimental string processing. Integration is available for every major editor and IDE through format-on-save, for pre-commit hooks to enforce formatting before code enters repositories, and for CI pipelines to fail builds on unformatted code. The tool handles complex Python constructs including decorators, type annotations, match statements, walrus operators, positional-only parameters, and parametrized generics. AST-based formatting ensures correctness, and the deterministic output means reformatting an already-formatted file produces no changes, enabling fast incremental checks in CI.

Tags & Categories

Statistics

6
Views
0
Clicks
0
Like
0
Dislike

Comments

Log In to post a comment

No comments yet. Be the first!