我使用flake8
与一堆插件(flake8-docstrings
,flake8-isort
,flake8-black
)。我把它们都预先安装在venv
中。
My repo to be check withpre-commit
:
- 根目录有两个包
- 每个都有自己的
pyproject.toml
(配置black
和isort
)setup.cfg
(配置flake8
和pydocstyle
)
├── foo
│ ├── pyproject.toml
│ ├── setup.cfg
│ └── (the package)
├── bar
│ ├── pyproject.toml
│ ├── setup.cfg
│ └── (the package)
└── venv
我想通过pre-commit
为这两个包调用flake8
。
我现在是这样做的:
---
repos:
- repo: local
hooks:
- id: flake8-foo
name: Run flake8 in foo package
entry: bash -c "cd foo && flake8"
language: python
- id: flake8-bar
name: Run flake8 in bar package
entry: bash -c "cd bar && flake8"
language: python
当我运行pre-commit run --all-files
时,foo
中有一个错误,它会多次打印相同的输出:
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
./path/in/foo/to/file.py:49:1: D401 First line should be in imperative mood
- 有更好的方法去做这件事吗?
- 不,我不愿意把这些包拆分成它们自己的仓库
- 如何让错误信息只打印一次?
pre-commit
在设计上操作文件,它也被优化为批量运行针对文件的多个进程
这里发生的事情是您的配置正在运行bash -c "cd bar && flake8" file1 file2 file3
等的几个调用(每个处理器约1次)。
幸运的是,有一个设置可以帮你解决这个问题:
pass_filenames: false
与:
---
repos:
- repo: local
hooks:
- id: flake8-foo
name: Run flake8 in foo package
entry: bash -c "cd foo && flake8"
language: python
pass_filenames: false
files: ^foo/
types: [python]
- id: flake8-bar
name: Run flake8 in bar package
entry: bash -c "cd bar && flake8"
language: python
pass_filenames: false
files: ^bar/
types: [python]
也就是说,通过使用repo: local
钩子,您将失去框架的大部分好处:
- 预提交不管理工具的安装(您的每个开发人员必须单独安装工具并在特定版本)
- 没有任何基于文件名的优化
- 如果你只更改了一个文件,那么你当前要检查整个存储库两次
- 在合并冲突期间,预提交优化要运行的文件(不是整个repo)
- 和
我建议您的单仓库设置仍然以正常方式调用flake8,但使用--config
,以便它适用于您的子仓库:
repos:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
name: flake8 ./foo/
alias: flake8-foo
files: ^foo/
args: [--config, foo/setup.cfg]
- id: flake8
name: flake8 ./bar/
alias: flake8-bar
files: ^bar/
args: [--config, bar/setup.cfg]
免责声明:我是预提交的作者,也是flake8的当前维护者