创建一个 .pre-commit-config.yaml
文件:
repos:- repo: https://github.com/psf/blackrev: '22.8.0'hooks:- id: blackargs:- --line-length=120- repo: https://github.com/pycqa/isortrev: '5.12.0'hooks:- id: isortname: isortfiles: "\\.(py)$"args:- --profile=black- repo: https://github.com/PyCQA/flake8rev: '3.8.4'hooks:- id: flake8args:- --ignore=F403,F405,W504,W503,E203- --max-line-length=120- repo: https://github.com/pre-commit/pygrep-hooksrev: v1.9.0hooks:- id: python-check-blanket-noqa- repo: https://github.com/pre-commit/pre-commit-hooksrev: v4.3.0hooks:- id: trailing-whitespace- id: end-of-file-fixer- id: check-added-large-filesargs: ['--maxkb=100', --enforce-all]- id: check-json- id: check-docstring-first- id: check-yaml- id: debug-statements- id: mixed-line-ending- repo: https://github.com/PyCQA/pylint/rev: v2.17.2hooks:- id: pylintname: pylintentry: pylintlanguage: systemtypes: [python]args:['--rcfile=.pylintrc','--disable=C0114,C0415,W0212,W0235,W0238,W0621,C0103,R1735,C2801,E0402,C0412,W0719,R1728,W1514,W0718,W0105,W0707,C0209,W0703,W1203',]
说明:当执行 git commit
时,pre-commit 会自动运行这些工具,如果代码格式不符合规范,会自动格式化。此时需要重新 add 和 commit 格式化后的代码。
参见:pre-commit