4

I want to use the Black formatter for my Python files, but not for my JSON settings.

I have these set in my settings.json:

    "python.formatting.provider": "black",
    "editor.formatOnSave": true,

I have tried to use the --exclude tag by adding the following to settings.json:

    "python.formatting.blackArgs": [
        "--exclude /*\\.json/"
    ],

which is equivalent to a commandline call with black --exclude /*\.json/

I also tried

    "python.formatting.blackArgs": [
        "--exclude /*\\.json/"
    ],

based on this post: VS Code Python + Black formatter arguments - python.formatting.blackArgs.

However, it is still formatting my settings.json.

Kyle A.
  • 93
  • 2
  • 5

4 Answers4

4

You can also disable the formatting for JSON, by:

  • going to the preferences with [ctrl+,] and disabling JSON > Format: Enable.
  • opening the settings.json file (open palette with [ctrl+shift+p] and search for "settings json") and add the following line:
    "json.format.enable": false
    

Or you can also limit the formatting to python files, by adding this setting in the settings.json file:

"[python]": {
   "editor.formatOnSave": true
 }
eymerich92
  • 43
  • 6
3

Black doesn't format JSON. What's happening is VS Code has it's own included JSON formatter and that's what is formatting your settings.json. Do you have a setting turned on like "editor.formatOnSave" turned on? If so then it sounds like you want to scope it to just Python files, e.g.:

"[python]": {
  "editor.formatOnSave": true
}
Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
1

Black does format JSON and for me, broke it:

╰─➤  black proj/
reformatted proj/schema.json
All done! ✨  ✨
1 file reformatted.

╰─➤  git diff proj/schema.json | wc -l
299
sqqqrly
  • 873
  • 1
  • 7
  • 10
  • For recursive searches, black will attempt to format any file that matches the `--include` regex, which defaults to `\.pyi?$`. Perhaps your project has a different configuration? – snakecharmerb Mar 02 '22 at 17:57
0

Black can and will break json files (as sqqqrly mentioned).

I generally only wanted to apply black formatting to python files, but I am setting up a CICD pipeline where I check for the git diff for files in a repo, which may include any number of file types, and it was going to be a bit fiddly to filter inside the pipeline.

Luckily, there's a flag --force-exclude we can use to enforce this (per the docs). It takes a regex, which we can use a negative lookahead to ensure only python files are formatted. (note: single quotes are required for bash, otherwise you get bash: !\.py: event not found)

[me@here]$ black /tmp/example.json --force-exclude '(^.*(?<!\.py)$)'
No Python files are present to be formatted. Nothing to do 

Also, because it excludes the file, it's not added to the file count even if other files are formatted, just in case that's important to you.

[me@here]$ black /tmp/a.json /tmp/b.py --force-exclude '(^.*(?<!\.py)$)'
All done! ✨  ✨
1 file left unchanged.
Edward Spencer
  • 448
  • 8
  • 10