Elena' s AI Blog

Linters and Git Pre-commit

11 Sep 2022 / 5 minutes to read

Elena Daehnhardt


Midjourney AI-generated art


Introduction

Coding can be hectic and also requires adhering to code styles. For instance, it is a good practice to comply with the PEP 8 guidelines for Python code. The PEP 8 standard allows us to write easy-to-read code, which is essential when we want to write reusable code and expect others to read/reuse our code while collaborating with other programmers. PEP 8 establishes rules about good variable names, commenting, and space usage, amongst other good style practices so helpful in writing reusable and professional code.

While PEP 8 is a standard, some tools can help us check and fix style issues. Flake8 is such a tool (amongst others such as Pylint and PyLama) that automatically inspects code for errors of PEP 8 compliance, see Flake8: Your Tool For Style Guide Enforcement. These tools are also called linters. In this post, we will use git hooks and pre-commit for a simple setup for checking Python code before committing files into the repository.

Linters

Linters help to check for code syntax errors and help to fix them. It is excellent to use linters to ensure code quality in the following aspects:

  1. ensure correct code formatting and style;
  2. minimise syntax errors;
  3. help in code reviews;
  4. saving development time.

Flake8 is easy to install with pip. Pip is the standard package manager for Python, and should you need guidance on how to install it, read the article at realpython.com. Since you are reading this post, I assume that, most likely, you have already installed pip on your system.

# define which Python version you want to install flake8 for
python<version> -m pip install flake8

To use flake8, you can run it on a specific file or directory.

# Run Flake8 on repo directory
flake8 path/repo
# Run Flake8 on the file main.py
flake8 path/repo/main.py

Code formatters

To reformat your code with PEP 8 guidelines, you can use Black (alone or together with Flake8). As it is mentioned on the Black website, black requires Python > 3.6.2 and installed with pip.

pip install black. 

The usage is similar to flake8; just feed in the project directory or file.

black <file_or_directory>

Overall, Back can help us reformatting python files following PEP 8 guidelines, while Flake8 checks for syntax and style errors. Using them together is a good practice, and they work quite well together in tandem. Refer to the docs “Using Black with other tools” for more details on Black’s compatibility with Flake8 and other linters.

Git Hooks and Pre-commit

We have an out-of-box solution for checking and reformatting our code before committing it to the repository. Git pre-commit package will help us perform these checks automatically while running Git commits.

Installation

To further install pre-commit in your Git repository folder, run the following commands.

# Install pre-commit
pip install pre-commit
# Install git hooks in your .git/ directory
pre-commit install

Configuration

To configure the hooks we want to include, we define the .pre-commit-config.yaml file in the project repository. Herein we define Git hooks for Black and Flake8.

repos:
  - repo: https://github.com/python/black.git
    rev: 19.10b0
    hooks:
      - id: black
        language_version: python3
  - repo: https://gitlab.com/pycqa/flake8.git
    rev: 3.7.9
    hooks:
      - id: flake8
        language_version: python3

When we install pre-commit, it creates a Git hook or a shell script located in the hidden .git/hooks directory of a Git repository. These scripts help in automating your development processes. As we see, there are already some Git hook scripts (files with .sample extension) are installed for our Git repository.

ls .git/hooks
Git hook files

To read Git Manual about githooks, run:

man githooks

Usage

In short, the pre-commit hook can automatically code style and formatting checks when committing. As a result, the commit will not execute when the pre-commit fails. We can also run pre-commit automatically with all files in the repository.

pre-commit run --all-files

There are sometimes issues we can’t fix at the moment; however, we need to commit files to the repository. Use the –no-verify option to get your commit through without running that pre-commit hook.

git commit -am "commit message" --no-verify 

Conclusion

We have learned about a simple Python code development cycle with Git pre-commit checks and code reformatting. We installed pre-commit for a Git repository and checked code for Flake8 and Black issues.

References

  1. Black
  2. Black website
  3. realpython.com
  4. Flake8: Your Tool For Style Guide Enforcement
  5. “Using Black with other tools”
  6. A framework for managing and maintaining multi-language pre-commit hooks
  7. How to Write Beautiful Python Code With PEP 8

Did you like this post? Please let me know if you have any comments or suggestions.

Posts about development tools and Python coding

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2022) 'Linters and Git Pre-commit', daehnhardt.com, 11 September 2022. Available at: https://daehnhardt.com/blog/2022/09/11/edaehn-git-pre-commit/
All Posts