Automating PEP 8 Style Checks with Git Pre-commit Hooks
A Git pre-commit hook is a script that runs automatically before each commit and can block the commit if a check fails. This post sets up pre-commit hooks with Python linters to enforce PEP 8 code style automatically before files are committed.
Coding gets hectic, and style is usually the first thing I drop when Iβm racing to finish something. Thatβs a mistake: PEP 8 is the official Python style guide, and it sets out rules for naming, commenting, indentation, and whitespace. Following it produces code thatβs easy to read and reuse β which matters a lot more once someone else has to work in your codebase.
PEP 8 is a standard, not an enforcement mechanism, so you need tooling to check and fix violations automatically. Flake8 is one such tool (alongside others such as Pylint and PyLama) that inspects code for PEP 8 compliance errors β see Flake8: Your Tool For Style Guide Enforcement. Tools like this are called linters. In this post, I use Git hooks and pre-commit for a simple setup that checks Python code before you commit files into the repository.
Python Linters: Flake8 for PEP 8 Compliance
A linter is a static-analysis tool that checks source code for syntax errors and style violations without executing it. Linters ensure code quality in the following aspects:
- ensure correct code formatting and style;
- minimise syntax errors;
- help in code reviews;
- save development time.
Flake8 is a Python linter that is easy to install with pip. Pip is the standard package manager for Python; if you need guidance on installing it, read the article at realpython.com. Since you are reading this post, you most likely already have pip on your system.
# define which Python version you want to install flake8 for
python<version> -m pip install flake8
To use Flake8, 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
Black is an opinionated Python code formatter that rewrites source files to a deterministic PEP 8-compliant style. You can use Black alone or together with Flake8. Per the official Black installation docs, Black requires Python 3.10+ to run and is installed with pip.
The usage is similar to Flake8; just feed in the project directory or file.
black <file_or_directory>
Black reformats Python files following PEP 8 guidelines, while Flake8 checks for syntax and style errors. Using them together is good practice, and they work well 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 the pre-commit Framework
pre-commit is a framework for managing and running Git hooks that checks and reformats code before it is committed to the repository. Once installed, it runs your configured checks automatically on every git commit. See the official pre-commit documentation for the full hook catalogue.
Final Thoughts: Git Pre-commit Workflow for PEP 8
π Subscribe to keep reading.
References
π Subscribe to keep reading.
Subscribe to unlock the full article β€οΈ
I keep most of the site completely open. A few unusually detailed tutorials need a free subscriber login so I can keep publishing this kind of work.
Log in to unlock
The form below signs you up for the newsletter. It does not log you into the app β log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.
Full content temporarily unavailable β refresh in a moment