Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know that Black is an opinionated formatter, but I love everything it does except one major thing. When I have a function with multiple arguments, instead of displaying it like this:

def example_function(arg_1: str, arg_2: bool, arg_3: int = 0, arg_4: int = 1, arg_5: float = 0.0):     pass 

I'd rather display it as follows for readability

def example_function(     arg_1: str,      arg_2: bool,      arg_3: int = 0,      arg_4: int = 1,      arg_5: float = 0.0 ): 

Is this achievable with Black or some other formatter? I have this problem several times and it makes me consider not to use Black, either something else or nothing at all. Any ideas or comments?

like image 647
Patrick Da Silva Avatar asked Dec 04 '19 20:12

Patrick Da Silva


People also ask

Is black the best Python formatter?

Black is the uncompromising Python code formatter. 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.

Can you break a black code?

Black will break a line before a binary operator when splitting a block of code over multiple lines. This is so that Black is compliant with the recent changes in the PEP 8 style guide, which emphasizes that this approach improves readability.

How do I install Python black format?

Black can be installed by running pip install black . It requires Python 3.6. 0+ to run. Once Black is installed, you will have a new command-line tool called black available to you in your shell, and you're ready to start!


1 Answers

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://github.com/psf/black/blob/master/docs/installation_and_usage.md#command-line-options

For example:

$ black --line-length 80 example.py 

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

https://github.com/psf/black/blob/master/docs/the_black_code_style.md#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".

like image 113
damon Avatar answered Sep 22 '22 17:09

damon