skip to content
~/bermudev/blog

Pre-commit Hooks in Python

/ 3 min read

Last month I was talking about code formatters: tools that help to unify styles in our code, making it cleaner, unified, and helping when working in a team.

Now, I’m going to talk about how to integrate those tools into our workflow, so that we make sure we always use them. For this we will use what is known as Pre-commit hooks.

Precommit logo

What are Pre-commit Hooks?

Pre-commit hooks are scripts or tools that run automatically before each commit is made in a version control system, such as Git. These hooks help developers catch potential issues early on, ensuring that code committed to the repository adheres to a set of predefined standards and passes various checks. Sounds familiar?

How do they work?

Pre-commit hooks are defined and configured in a file called .pre-commit-config.yaml at the root of your project. This YAML file lists the hooks you want to run and the commands needed to execute them. Each hook can be a linter, code formatter (for example Black or YAPF), static code analyzer, or any other tool that helps maintain code quality.

When you attempt to make a commit, the pre-commit hook is triggered, and the defined checks are performed on the staged files. If any issues are detected, the commit is halted, automatically fixing the problem for you or allowing you to address the problems before finalizing the commit.

Precommit run

What are the benefits of Pre-commit Hooks?

Using pre-commit hooks offers several benefits for developers and development teams:

  1. Consistent Code Style: Enforcing code style guidelines with pre-commit hooks ensures that all team members follow the same coding standards, leading to a more consistent codebase.

  2. Early Issue Detection: Pre-commit hooks catch potential issues, such as formatting errors, linting problems, or security vulnerabilities, before they get into the codebase, preventing future headaches.

  3. Improved Code Review Process: By catching issues early, pre-commit hooks reduce the number of code review comments related to code style or basic mistakes, making the review process more focused on higher-level design and logic.

Setting Up Pre-commit Hooks in Python

Setting up pre-commit hooks in your Python project is really easy, just follow these steps:

  1. Install pre-commit: Run pip install pre-commit to install the pre-commit package.
  2. Create the .pre-commit-config.yaml file: Define the hooks you want to use and specify the commands for each hook. An example of such a file could be:
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-yaml
      - id: debug-statements
      - id: double-quote-string-fixer
      - id: name-tests-test
      - id: requirements-txt-fixer
  - repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
      - id: flake8
  - repo: https://github.com/psf/black
    rev: stable
    hooks:
      - id: black
  1. Install the hooks: Run pre-commit install to install the hooks into your Git repository.
  2. Run the hooks: Pre-commit hooks will now automatically run before each commit. If any issues are found, you will need to address them before proceeding with the commit.

That’s all, from now on pre-commit will be launched with every commit, and with it all the tools that we have indicated in the configuration file!