Skip to content

klieret/python-pre-commit-demo-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Exercises: pre-commit for python projects

PR welcome

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.

📦 Exercise setup

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

🔥 Exercises

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.

  1. Replace return a + b with return None (mypy will complain!)
  2. Remove spaces between + (black will automatically reformat the file and add the spaces back)
  3. Change the to teh in the docstring (codespell will flag it as a spelling mistake)
  4. Import math (flake8 will complain about an unused iport)
  5. 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) or git reset --hard (don't do that on an important repository without understanding the implications).