The main aim of this repository is to act as a guide on how to configure the pre-commit hooks in your existing python projects.
Pre-commit hooks are small “scripts” that are run locally on staged files when
using the git commit
command. Basically these scripts run each time right when
you try to commit your code locally. We can set configurations such that code
quality is checked each time we attempt to commit the code and commit is
prevented if the pre-defined standards are not met. This ensures that only
quality code gets staged and is pushed to the repository.
Currently we are using four packages in our hooks to format and lint our code. The packages included are as follows in the same sequence as here:
- Black - automatically formats each python file as much as possible
- isort - automatically sorts imports alphabetically, separated into sections and by type
- flake8 - displays warning in case of violations and code smells
- pylint - displays warning about code smells, format and even documentaion
The reason black is placed before flake8 and pylint is to make sure they catch as few warnings as possible, since black formats everything beforehand.
pip install pre-commit
pip install pylint
Copy the following files from this project to your own project's root directory
- .pre-commit-config.yaml
- .flake8
- pyproject.toml
Run these commands within your virtual environment:
pre-commit install
Note that pre-commit runs only on files that you have already staged;
meaning that you have already performed the git add
command on them
So if you have not staged your changes yet, you can do so with the following
command
git add .
Now the pre-commit script will check your project's formatting when you attempt
to commit your code with git commit
. Commit is approved only when the pre-commit script finds your
code well formatted and documented.
Note: You can also run the pre-commit script without attempting to commit (for test purposes). Use the command below to run the pre-commit script manually.
pre-commit run --all-files
Important Note: Sometimes when you commit your code, black or isort could make changes to your files
making them unstaged, specially when the commit failed on pre-commit checks. In such case, you
need to stage those files back with git add
and then commit again with git commit