The issue is that you need =80 instead of 80 after --line-length for version 1.38.1 and above:
--line-length=80

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.
Videos
» pip install black
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".
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
):
Hey!
I'm curious how others handle the following situation or what is the "proper" way?
So I use Black as code formatter and it's awesome, but in certain situations also really annoying. I have written a script to convert video files (mp4, mkv, etc.) to avi (divx/xvid) because my toaster DVD player cannot play anything else on the USB port.
The script is not the problem, it works great but the issue is that I use subprocess to call ffmpeg. Subprocess wants the command line arguments as list elements and everyone who ever used ffmpeg via console knows that this program wants A LOT of arguments. And this is where the problem with Black comes in. I have to forward 26 arguments and all written in one line sum up to 248 characters. (I already use several vars, e.g. output file path, otherwise the line would be closer to 400 chars) Black is telling me that this is a bit long for one line and "fixes" this problem for me.
The result is that Black puts every single list element on its own line, many of the elements are only 2 to 4 characters long. This results in practically a whole empty page with some characters in the wild in the middle of my code. This is just as bad for readability as one very long line.
My question here is, how do you handle this situation? Do you …
obey the Black rules and let it put every element on a single line even this result in a empty page in middle of your code?
increase the line length to a very high number to get rid of the problem?
resort to multiple variables in which you put parts of the argument list and then concatenate the variables in the subprocess run function?
do you temporarily deactivate Black (you can put #fmt: off before and #fmt: on after the line to ignore) for that line of code to leave it long as it is?
any other solution?
Obviously Black planned for such a case, because it offers the option to exclude a code section from formatting. On the other hand, it doesn't automatically make use of it or offers this solution but rather line-break the list.
For the moment I have set my line-length to 500 because I don't share code, so it doesn't affect others.
You can use the --skip-string-normalization option at the command line, or in your Visual Studio Code options.
See The Black code style, Strings.
For example:
{
...
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--skip-string-normalization",
"--line-length",
"100"
]
...
}
I don't know how it works for you all of that:
{
...
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--skip-string-normalization"
]
...
}
or
{
...
"python.formatting.blackArgs": [
"-S"
],
...
}
I've tried everything and THE ONLY works for me is:
"black-formatter.args": ["-S"]
And VSCode for some reason installed via pip install -U black , don't know it is crucial or not.