LoginSignup
0
1

PythonにおけるLinterとFormatterのテンプレ

Last updated at Posted at 2023-01-29

プロジェクト毎にLinterやFormatterの設定をするのはめんどくさいので、いつでも使い回せるように、pipを使ってpythonのテンプレートを作っておこうと思います。

ツール

以下のパッケージを使用しています。

  • black
  • isort
  • flake8

設定ファイル

pyproject.toml
[tool.black]
line-length = 88

[tool.isort]
profile = "black"
line_length = 88
.flake8
[flake8]
max-line-length = 88
extend-ignore = E203
.vscode/settings.json
{
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": false,
  "python.linting.flake8Enabled": true,
  "python.linting.lintOnSave": true,
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
      "--line-length",
      "88"
  ],
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
      "source.organizeImports": true
  }
}
Makefile
install:
	pip install --upgrade pip
	pip install -r requirements.txt

uninstall:
	pip install --upgrade pip
	pip freeze | xargs pip uninstall -y

freeze:
	pip install --upgrade pip
	pip freeze > requirements.txt

format:
	black src
	isort src

lint:
	flake8 src

使用例

まず、必要なライブラリをインストールします。

zsh
pip install black isort flake8

適宜、FormatterやLinterを実行します。

zsh
make format
# or 
make lint

参考

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

0
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
0
1