Uncompromising Python Formatting

I wanted to give a shout out to Black, “the uncompromising Python code formatter.”


It really is liberating not to have to worry about code formatting, not having to have unproductive, sterile discussions over PRs about code minutiae and yet being reassured that all your code is nicely formatted; most importantly, the quality of the style is not developer-dependent (come on, who has never worked with a lazy slob who cannot quite get into his –it’s always a “he”– thick skull that long lines and messy code generate bugs like a nest of cockroaches?)

From their project page:

By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.

To use it, it’s super simple: in your Pipfile use:

black = "==18.9b0"

(pinning to a release is necessary, until Issue 517 is resolved, or use the --pre flag), then run:

pipenv install --dev

and to run the autoformatter:

pipenv run black -l 100 src tests

assuming that your Python code lives in the src and tests folders; this also modifies the line length to be 100 columns.

We would also recommend to add this to a pre-commit hook (the script in .git/hooks/pre-commit) so that the code gets automatically formatter before heading to a PR (and then people will make fun of you because you forgot to run black).

Happy (clean) Coding!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s