Using pre-commit hooks for your Python project

We all forget to format, lint, and make sure print statements are removed.

Let’s be honest - we’ve all forgotten to format our code or checked random print debugging statements into our code base. There’s a way to prevent this from happening!

This is where pre-commit hooks come in. A pre-commit hook is a small script that runs automatically before you commit your code using git.

It allows you to catch issues early, like syntax errors, unused imports, or inconsistent formatting without needing to run these checks yourself.

Why use pre-commit hooks?

Using pre-commit hooks is a way to automate the boring things. Once you set these up, you’ll wonder how you’ve ever worked without them.

Using pre-commit hooks allows you to:

  • Remain consistent with formatting, styling, etc.

  • Catch errors before the code review process starts

  • Prevents mistakes like committing print statements or large data files

  • Have peace of mind by offloading the reminder to do these things.

Standard Packages to Include

These tools are some of the most common to use in a pre-commit hook:

  • black - Auto-formats your code (docs)

  • flake8 - Linter for style and basic errors (docs)

  • isort - Automatically sorts imports (docs)

  • mypy - Static type checker (docs)

  • bandit - Security linter (docs)

Other useful hooks

While the above are generally standard across projects, some other ones you may want to consider adding in as part the pre-commit repository:

  • detect-aws-credentials - prevents committing AWS keys accidentally

  • check-json/check-yaml - validates config files

  • debug-statements - Blocks commits that include print or pdb.set_trace()

  • check-merge-conflict - Stops commits with leftover conflict markers

Creating a pre-commit hook

To create a pre-commit hook, create a yaml file in the root of your repository called .pre-commit-config.yaml. Copy, paste, and modify this as you see fit:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-merge-conflict

  - repo: https://github.com/psf/black
    rev: 24.3.0
    hooks:
      - id: black

  - repo: https://github.com/PyCQA/flake8
    rev: 6.1.0
    hooks:
      - id: flake8

  - repo: https://github.com/pre-commit/mirrors-isort
    rev: v5.12.0
    hooks:
      - id: isort

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.6.1
    hooks:
      - id: mypy

To install it, be sure to install a package:

>>> pip install pre-commit
[output suppressed]
>>> pre-commit install

Now, every time you type git commit, these tools will automatically run on the staged files.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.