Working remotely as a Software Engineer has taught me that in order for a feature to be delivered well, people should be able to code review your work properly focusing mostly on the logic you did rather than the small nitty-gritty things. Good thing we have packages such as Black, Flake8 and optionally Mypy (if you want to include static typing) to check for us, The only thing missing is automating the process.
One thing we can use are Git hooks, specifically pre-commit hooks as I want to perform the automated checks before someone commits to the repository. the only challenge was how to share this with your teammates, and here comes python’s pre-commit package.
Using pre-commit is relatively easy, below are the steps you can follow:
- Install it using pip:
pip install pre-commit
- Create a
.pre-commig-config.yamlfile containing your desired hooks
pre-commit installto install your pre-commit hook in git
Here’s a sample
.pre-commig-config.yaml that contains Black, Flake8, and MyPy.
You can add additional configurations in the yaml file pertaining to each of your hooks. In the example above we edited
max-line-length since Black has a limit of 90 characters instead of 88.
To test here’s a sample file we named
Now let’s try to add and commit this to our local repository by doing
git add then
We can see that our hooks are working! Hope this helps and here’s a repo of the setup so you can freely test.
As an added bonus you may want to check what hooks other open source projects use on their development pipeline. You can learn a lot! Here’s the one from flask.