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 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:
- ensure correct code formatting and style;
- minimise syntax errors;
- help in code reviews;
- 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
# Run Flake8 on the file main.py
pip install black.
The usage is similar to flake8; just feed in the project directory or file.
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.
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
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.
- repo: https://github.com/python/black.git
- id: black
- repo: https://gitlab.com/pycqa/flake8.git
- id: flake8
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.
To read Git Manual about githooks, run:
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
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.
- Black website
- Flake8: Your Tool For Style Guide Enforcement
- “Using Black with other tools”
- A framework for managing and maintaining multi-language pre-commit hooks
- 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
Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.