Skip to content

Customize static analysis behavior静的解析の挙動をカスタマイズ


You can fully alter the static analysis performed by the fmt command by modifying the reserved environment named hatch-static-analysis. For example, you could define the following if you wanted to replace the default behavior with a mix of Black, isort and basic flake8:予約された環境であるhatch-static-analysisを変更することで、fmtコマンドによって実行される静的解析を完全に変更できます。たとえば、デフォルトの挙動をBlackisort、および基本的なflake8の組み合わせに置き換えたい場合、次のように定義できます:

[tool.hatch.envs.hatch-static-analysis]
dependencies = ["black", "flake8", "isort"]

[tool.hatch.envs.hatch-static-analysis.scripts]
format-check = [
  "black --check --diff {args:.}",
  "isort --check-only --diff {args:.}",
]
format-fix = [
  "isort {args:.}",
  "black {args:.}",
]
lint-check = "flake8 {args:.}"
lint-fix = "lint-check"
[envs.hatch-static-analysis]
dependencies = ["black", "flake8", "isort"]

[envs.hatch-static-analysis.scripts]
format-check = [
  "black --check --diff {args:.}",
  "isort --check-only --diff {args:.}",
]
format-fix = [
  "isort {args:.}",
  "black {args:.}",
]
lint-check = "flake8 {args:.}"
lint-fix = "lint-check"

The format-* scripts correspond to the --formatter/-f flag while the lint-* scripts correspond to the --linter/-l flag. The *-fix scripts run by default while the *-check scripts correspond to the --check flag. Based on this example, the following shows how the various scripts influence behavior:format-*スクリプトは--formatter/-fフラグに対応し、lint-*スクリプトは--linter/-lフラグに対応します。*-fixスクリプトはデフォルトで実行され、*-checkスクリプトは--checkフラグに対応します。この例に基づいて、さまざまなスクリプトが挙動にどのように影響するかを示します:

Commandコマンド Expanded scripts拡張スクリプト
hatch fmthatch fmt
  • flake8 .flake8 .
  • isort .isort .
  • black .black .
hatch fmt src testshatch fmt src tests
  • flake8 src testsflake8 src tests
  • isort src testsisort src tests
  • black src testsblack src tests
hatch fmt -fhatch fmt -f
  • isort .isort .
  • black .black .
hatch fmt -lhatch fmt -l
  • flake8 .flake8 .
hatch fmt --checkhatch fmt --check
  • flake8 .flake8 .
  • black --check --diff .black --check --diff .
  • isort --check-only --diff .isort --check-only --diff .
hatch fmt --check -fhatch fmt --check -f
  • black --check --diff .black --check --diff .
  • isort --check-only --diff .isort --check-only --diff .
hatch fmt --check -lhatch fmt --check -l
  • flake8 .flake8 .