静态代码检测
工具:Flake8
安装:
pip install flake8
PyCharm中配置:
执行代码检查:
执行结果:
(代码规范可参考 https://legacy.python.org/dev/peps/pep-0008/
错误代码说明可参考 https://pep8.readthedocs.io/en/latest/intro.html#error-codes)
Git提交代码检查
工具:pre-commit
安装:
pip install pre-commit
建立git hook:
在需要检查的git项目下执行
pre-commit install
添加配置文件:
在git项目根目录下创建*.pre-commit-config.yaml*文件
文件内容结构如下
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
(具体参数可参考 https://pre-commit.com/#pre-commit-sample-config)
手动执行检查:
pre-commit run --all-files
自动执行检查:
git执行commit命令时会自动执行检查