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-lengthwith 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".
Answer from damon on Stack OverflowThis 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-lengthwith 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".
This is now possible by adding a trailing comma to your last argument.
In your example you would write instead:
def example_function(
arg_1: str,
arg_2: bool,
arg_3: int = 0,
arg_4: int = 1,
arg_5: float = 0.0, # <-- Notice the trailing comma
):
» pip install black
Python devs that use Black for formatting. When I format with VSCode's black, it ignores the problem of very long lines and formats this way. Does it happen to anyone else?
Do you use black with its default line length of 88?
Black Fails to Format Single String Longer Than Line Length Limit
VS Code Python + Black formatter arguments - python.formatting.blackArgs - Stack Overflow
Videos
I was curious to hear how others deal with this. Do you use the black-default line length of 88 or do you override it to e.g. 79 (pep8 standard)?
I personally have now adjusted my linters to use line length 88 and using black with all its defaults. However, I am not completely sure whether I want it this way, or if I would prefer to keep linters at their defaults (79) and tell black to use that. I can tell that my scripts become a little shorter, at the expense of vertical screen real estate (I like to keep three editor tabs opened, side by side). If you are using black, what are your settings like, and why?
EDIT: I went back to using yapf and line length 79 for now. Reasons being yapf can format on paste, black replaces single quotes with double quotes (this can be turned off, I know), a strange method args formatting choice in black ...and I'm still not convinced the few extra line length chars makes sense to me right now, as so many other things expect 79 and not 88. Maybe I will revisit black later on, once it's out of beta, is more widely adopted/accepted and has a more official blessing (although it being developed by a core dev)...
EDIT 2: After having learned of some yapf limitations and quirks, I switched back to back. But I'm keeping my line lengths at 79 for now. I like the zero-configuration approach to black (although now I am using it with --line-length 79) and I guess I would get used to it. But I can't get behind the 88 line length until there is some type of recognition of this in a future pep, as I personally like 79 just fine and it really is a standard today.
The issue is that you need =80 instead of 80 after --line-length for version 1.38.1 and above:
--line-length=80

Hopefully the answer for a more recent VSCode and Black helps:
With VsCode Insiders (1.83.0-insider) and Black installed from extensions (v2023.4.1), installed from extensions (https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)
I had to add -l or --line-length and 80 as separate items to File->Preferences->Settings->[type]black->Black-formatter: Args->Add item.
In user settings json (Ctrl + Shift + P --> Open User Settings) I have:
"black-formatter.args": ["--line-length", "80"]
If this doesn't work, there's useful information in the Output Window (you can select Black Formatter) to see the logs from Black.
» pip install black