In Python, how to tweak Black formatter, if possible?

This is due to the default line length for black being longer than you’d like – 88 characters per line.

To decrease the line length, you can use the --line-length flag as documented here:

https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html

For example:

$ black --line-length 80 example.py

Black explains the --line-length setting in more detail here:

https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length

Line length

You probably noticed the peculiar default line length. Black defaults
to 88 characters per line, which happens to be 10% over 80. This
number was found to produce significantly shorter files than sticking
with 80 (the most popular), or even 79 (used by the standard library).
In general, 90-ish seems like the wise choice.

If you’re paid by the line of code you write, you can pass
--line-length with a lower number. Black will try to respect that. However, sometimes it won’t be able to without breaking other rules.
In those rare cases, auto-formatted code will exceed your allotted
limit.

You can also increase it, but remember that people with sight
disabilities find it harder to work with line lengths exceeding 100
characters. It also adversely affects side-by-side diff review on
typical screen resolutions. Long lines also make it harder to present
code neatly in documentation or talk slides.

Emphasis on the final paragraph.

I’d recommend just keeping the default settings. The beauty of Black is that it chooses for you, and therefor preempts any arguments about which way is “best”.

Leave a Comment