1
1

More than 1 year has passed since last update.

vscodeに導入したblackが機能しない

Posted at

はじめに

これこれを参考にflake8とBlackを導入したものの、保存時にblackの自動整形が行われない

状況

setting.jsonを以下の様に設定

settings.json
{
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.lintOnSave": true,
    "python.formatting.provider": "black",
    "editor.formatOnSave": true,
    "editor.formatOnPaste": false,

    "python.linting.flake8Args": [
        "--max-line-length=119",
        "--ignore=E203,W503,W504"
    ],
    "python.formatting.blackArgs": [
        "--line-length=119"
    ],
} 

以下のコードの保存時にフォーマットが行われない。

sample.py
 def foo():
    print(
                "Hello"
        "World"
        )

ただし、コマンドライン上でblackを実行するとちゃんと整形される。

$ black sample.py

原因

flake8ArgsとblackArgsの引数の指定方法が間違っていました。
こちらの記事の方法を参考に修正したものが以下

settings.json
{
    "python.linting.flake8Args": [
        "--max-line-length",
        "119",
        "--ignore=E203,W503,W504"
    ],
    "python.formatting.blackArgs": [
        "--line-length",
        "119"
    ],
}


ポイントは最大文字数の指定箇所を分割する事でした。
以上。

参考文献

Blackできれいに自動整形!flake8とBlack導入と実行

Pythonのコードフォーマッターについての個人的ベストプラクティス

【VS Code】BlackとFlake8を使ってきれいなPythonコードを書く!!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1