跳转至

自定义静态分析行为


可以通过修改保留的 环境配置 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"
  • format-* 脚本对应 --formatter / -f 选项。
  • lint-* 脚本对应 --linter / -l 选项。
  • 默认运行的是 *-fix 脚本。
  • --check 标志对应 *-check 脚本。

基于上述配置,以下命令行为及其展开脚本如下:

命令 展开脚本
hatch fmt
  • flake8 .
  • isort .
  • black .
hatch fmt src tests
  • flake8 src tests
  • isort src tests
  • black src tests
hatch fmt -f
  • isort .
  • black .
hatch fmt -l
  • flake8 .
hatch fmt --check
  • flake8 .
  • black --check --diff .
  • isort --check-only --diff .
hatch fmt --check -f
  • black --check --diff .
  • isort --check-only --diff .
hatch fmt --check -l
  • flake8 .