Recommended pre-commit checks for Python
For my projects I use this pre-commit template. In the past I’ve also integrated pylint
and mypy
, but these make committing slow for big projects so I’ve kept only the simple and fast checks and moved the others to CI.
There are 3 different checks:
- isort: sorts the imports according to the convention. If you are doing something special that needs a specific
import order, you can exclude the files here or in themselves by prepending
# isort: skip_file
. Notice that the--profile black
argument is needed to avoid any incompatibilities with black. - black: formats the code.
- a collection of pre-defined hooks: There are more in the repository but I consider these to be basic and useful for most projects.
default_language_version:
python: python3.9
exclude: 'exampledir|example.py'
repos:
- repo: https://github.com/pycqa/isort
rev: 5.9.3
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 21.8b0
hooks:
- id: black
exclude: "example.py"
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements
- id: detect-private-key
Leave a comment