Improve the Code Quality in Python Projects using Pre-Commit Hooks

Muhammad Urwatil Wutsqo
4 min readFeb 28, 2022

--

Pre-commit hooks help identify simple issues before submission to code review.

Let’s say you’re working on a group project and wanted to enforce a specific code style check, in this case, Black code-formatter. Usually, when doing the project alone, you do that manually or automatically using your IDE plugins. But when you’re doing that with other people, things become complicated. Everyone has a different code style, and not everyone uses the same IDE configurations as yours.

Here’s where the pre-commit comes to the rescue! To put it simply, right before you commit files, it will check your code based on some configurations. If it passes, your files are being committed. If not, it will try to fix it automatically, and if it fails, it will ask you to resolve your issues, or else you can’t commit your changes.

Getting Started

Installing pre-commit

You can install it using pip:

pip install pre-commit

Then add the following to your requirements.txt file (if you use one):

pre-commit

Creating configuration file

  1. Create a file named .pre-commit-config.yaml
  2. You can fill the file with this sample config:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: no-commit-to-branch

What is does basically is to check your code based on the specified hooks, in this case:

  • trailing-whitespace will check if there’s a trailing whitespace at every end of lines in your changes.
  • check-added-large-files will prevent you for committing large files into the repository.
  • no-commit-to-branch will prevent you from committing to specific branches. By default, those branches are main and master .

Those are some basic implementations of pre-commit. Now we want to add Black code-formatter to enforce our code style. We can do so by adding more config to the file:

-   repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
args: [“ — line-length=100”]

We can also customize the hooks by adding args parameter. In this case, Black usually limit the length of the lines to 88 characters, but I want it to be 100 characters. We can do so by adding args: [“ — line-length=100”] .

The final configuration file will look like this:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: check-added-large-files
- id: no-commit-to-branch
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
args: [“ — line-length=100”]

Install the git hook scripts

The configuration is done, now we can install the hooks script by doing so:

pre-commit install

Let’s check if it works

Here I will use my project from software development course as an example. It uses a slightly different configurations but the general idea of how this works is the same.

Let’s run git status to check the status of the current repository. We can see that we’re currently on the main branch.

git status

Let’s try commit the changes:

failed commit

It fails to commit the changes because the last hook isn’t passed which is the no-commit-to-branch. The hook will prevent you from committing directly from the main branch.

Now let’s try to switch to a new branch, and try to commit again.

We can see that it passed all the checks, and the commit attempt is done successfully.

Closing

It may not be able to detect complex issues in the codebase. But having it installed can save us some time from detecting simple issues in the code review like inconsistent code styles, so we can be more focused on the algorithms or logic of the application itself.

References

https://pre-commit.com/index.html

--

--

Muhammad Urwatil Wutsqo
Muhammad Urwatil Wutsqo

No responses yet