Eight must have pre-commit hooks for #python code

Recently there was an article on LinkedIn about how debugger statement was left in the production code (front end javascript) and thus user could not move forward.

People commented about the code review.

But why invest manual efforts when such things can be automated ? See #4 in the list below.

pre-commit hook is an under appreciated (dare I say, unknown) feature of git

As the name suggests, set of commands/scripts can be executed before git commit

If any of these fail (exit code other than 0) the code is not committed.

Some of these are just checks, but some automatically fix/change your code for the better. How cool is that.

Here are the eight precommit hooks you must use:

  1. Automatically remove trailing spaces
  2. Exactly 1 new-line at the end of file
  3. Check YAML (prevent bad config getting into production)
  4. debug statements are only for local testing, but get rid of them before committing to git
  5. reorder imports alphabetically (and group them)
  6. Automatically use latest idiomatic python. (pyupgrade)
  7. automatically fix style errors to make code look consistent
  8. catch type mismatches during development, leading to fewer runtime errors

While some of these may seem like they do not add value (consistent looking code) others have tremendous value (detect debugger statements, type mismatches and automatically using latest idiomatic python)

While these are specific to python, similar ones are available for other languages as well (git is programming language neutral). e.g. there is husky for node/javascript.

Read the original article here