If you haven’t heard of pre-commit and are using git, please read.

Pre-commit is a pip package which works like spell-correct on your code. Once you have it set up, whenever you try and do a git commit, it comes along and says:

“Woah there, you have a mistake here! Fix it first and I’ll let you pass.”

Gandalf: You shall not pass!

It even fixes some things for you; all you have to do is re-add the file and re-commit.

git add new_file.py
Git commit -m "helpful comment"

>>> Pre-commit - Failed 

Git add new_file.py
Git commit -m "helpful comment"

>>> Pre-commit - Passed

Pre-commit checks:

  • black and Flake8 - these are like an English teacher telling you when you have a comma in the wrong place *
  • Mypy - lets you know if you imported a package and then never used it. Also makes sure your Typing is properly adhered to*.
  • Isort - puts your imports in alphabetical order
  • jupyter-nb-clear-output - checks if you remembered to delete your outputs before committing.

*Mypy and flake8 failures don’t fix themselves, but it will tell you the line that needs fixing and why

Why I love and hate pre-commit:

  • It finds all my errors before my colleagues do a code review, and means I have one less thing to think about.
  • I rarely get a pass first time and fixing my typing is a pain.

Note: To use, you need to “pip install” it onto your server, and then “pre-commit install” the first time you’re in a new your git repository [link].