gitmyhub

black

Python ★ 42k updated 3d ago

The uncompromising Python code formatter

Black is a Python code formatter that automatically rewrites your code to one consistent style with almost no configuration, so your team stops arguing about formatting and starts shipping.

Pythonsetup: easycomplexity 1/5

Black is the "uncompromising" Python code formatter — a tool that automatically rewrites Python source files to conform to a single, consistent style. The word "uncompromising" is the key to understanding its design philosophy: unlike older linters or formatters that offer dozens of configuration options for line length, quote style, bracket placement, and so on, Black has very few settings. It makes all the decisions for you.

The problem Black solves is the endless, low-stakes debate that happens on development teams about code style. Should strings use single quotes or double quotes? Where should long function calls be split across lines? How many blank lines between methods? These questions consume surprising amounts of attention during code review without actually improving the software. Black's answer is: stop arguing, just run the formatter.

Here is how it works: you run black myfile.py or point it at a directory, and Black reformats the files in place using its opinionated rules. It parses the Python source into an abstract syntax tree (a structural representation of the code), then regenerates the code from scratch in Black's canonical style — normalizing indentation, line lengths, quote style, trailing commas, and more. As a safety check, Black verifies that the reformatted code produces an equivalent AST to the original, so it cannot accidentally change what the code does.

Because Black's output is deterministic — the same input always produces the same output — it eliminates formatting-related noise in diffs and code reviews. Two developers can independently edit the same file, run Black, and get identical results.

You would use Black as part of any Python project's development setup, typically integrated with a pre-commit hook or CI pipeline so that all code is automatically formatted before it is committed.

The stack is Python 3.10 or later, installable via pip.

Where it fits