To run black code formatter, flake8 linter, as well as other hooks
(e.g., pretty-format-json) automatically each time something is committed to a repo:
Install
pre-commiton the local computer (in macOS can usebrew install pre-commit, in linux can install in a virtualenv usingpip install pre-commit)Add a
.pre-commit-config.yamlto the root of the repository:repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.4.0 hooks: - id: check-json - id: pretty-format-json args: [--no-sort-keys, --autofix, --no-ensure-ascii ] - repo: https://github.com/ambv/black rev: 20.8b1 hooks: - id: black language_version: python3 - repo: https://gitlab.com/pycqa/flake8 rev: 3.8.4 hooks: - id: flake8Add a
pyproject.tomlfile forblackconfiguration:[tool.black] line-length = 88 include = '\.pyi?$' exclude = ''' /( \.git | \.hg | \.mypy_cache | \.tox | \.venv | _build | buck-out | build | dist )/ '''Add a
.flake8configuration file:[flake8] ignore = E203, E266, E501, W503, F403, F401 max-line-length = 88 max-complexity = 18 select = B,C,E,F,W,T4,B9Install git hooks in the repository .git/ directory:
> pre-commit install