pythonvisual-studio-codepython-black

VS Code Python + Black formatter arguments - python.formatting.blackArgs


April 2024 UPDATE, Please read:

It appears the answer since late 2023 is the newly accepted answer provided by Mircea, this question is old so older, now incorrect answers, have more upvotes but please scroll down for that answer.

original question:
I'm using the May 2018 Python extension (released June 2018) for VS Code 1.23.1 on Windows, python 3.6 via Anaconda, conda installing black from conda-forge into my conda environment.

In my user settings.json I have the below:

"python.formatting.blackArgs": [
    "--line-length 80"
],

which I'd think would be the correct way to structure this to pass arguments to black in VS Code Python formatting.

However, in my python Output pane I get the below:

Formatting with black failed.
Error: Error: no such option: --line-length 80

If I edit my settings.json to be no args, such as:

"python.formatting.blackArgs": [],

black works as expected.

Does anyone know how to pass arguments correctly to the new (as of June 2018) black formatter?


Solution

  • 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.