Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 

EDIT: 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?

like image 557
Rafael Zayas Avatar asked Jun 06 '18 17:06

Rafael Zayas


People also ask

Is the Black formatter supported in Visual Studio Code?

A Visual Studio Code extension with support for the black formatter. The extension ships with black=22.6.0. This extension is supported for all actively supported versions of the python language (i.e., python >= 3.7). The bundled black is only used if there is no installed version of black found in the selected python environment.

What is the Black formatter extension for Python?

This extension is experimental. The plan is that it will eventually replace the black formatting functionality of the Python extension. Once installed in Visual Studio Code, "Black Formatter" will be available as a formatter for python files. Please select "Black Formatter" (extension id: ms-python.black-formatter) as the default formatter.

How to format Python code in VSCode?

Install Microsoft's Python extension in VSCode: Open your VSCode settings, by going 'Code -> Preferences -> Settings'. Search for "python formatting provider" and select "black" from the dropdown menu: In the settings, search for "format on save" and enable the "Editor: Format on Save" option: Black will now format your code whenever you save ...

How do I use black in VSCode?

Black is "the uncompromising Python code formatter." It can be configured to automatically format your code whenever you save a file in VSCode. Install Black in your virtual environment: Open your VSCode settings, by going 'Code -> Preferences -> Settings'. Search for "python formatting provider" and select "black" from the dropdown menu:


2 Answers

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

--line-length=80   

enter image description here

like image 107
Pavel Hanpari Avatar answered Sep 21 '22 16:09

Pavel Hanpari


The examples of formatter-specific settings show the following:

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"], "python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 20}"] 

So try:

"python.formatting.blackArgs": ["--line-length", "80"] 
like image 35
Keith Devens Avatar answered Sep 19 '22 16:09

Keith Devens