This repository demonstrates how you can catch a lot of python issues with pre-commit.
Part of the lecture Everything you didn't know you needed.
Clone the demo repository
git clone https://github.com/klieret/python-pre-commit-demo-tutorial.git
cd python-pre-commit-demo-tutorial
If you haven't done already, install the pre-commit
package:
pip3 install pipx
pipx install pre-commit
Let's take a look at the pre-commit
configuration from the repository:
cat .pre-commit-config.yaml
Let's install these hooks:
pre-commit install
Now let's open the test.py
file in your favorite editor.
Try one/each of the following modifications to test.py
.
After every modification, try to commit your change (git commit -a
) and
see what happens.
Alternatively, you can also simply run pre-commit run -a
to run pre-commit
over all files.
- Replace
return a + b
withreturn None
(mypy
will complain!) - Remove spaces between
+
(black
will automatically reformat the file and add the spaces back) - Change
the
toteh
in the docstring (codespell
will flag it as a spelling mistake) - Import
math
(flake8
will complain about an unused iport) - Add additional whitespace at the end of any line (
end-of-line-fixer
will fix it for you)
Note: If you ever get stuck, run
git stash
("stashes" away your changes) orgit reset --hard
(don't do that on an important repository without understanding the implications).