Integrate `wemake-python-styleguide` in pre-commit git hook

pre-commit git hook is a very useful tool to run linter, formatter, or complexity checker for your code before committing it. A popular framework is pre-commit, which, with a few easy steps, allows you to set up linting/formatting/checking tools for pre-commit hook. However, the pre-commit framework does not support all the tools for linting/formatting/checking purposes. For instance, wemake-python-styleguide doesn’t seem to be supported by pre-commit . Therefore, we will have to configure pre-commit git hook manually if you want to use wemake-python-styleguide, instead of the flake8 provided by pre-commit, for linting and checking your python code.

To integrate wemake-python-styleguide in pre-commit git hook, first follow the instructions on https://github.com/wemake-services/wemake-python-styleguide to install wemake-python-styleguide. Then open <your_local_repo>/.git/hooks/pre-commit.sample with your favorite text editor. This is the script git will run after you give the git commit command. If this script exits with code 1, your commit would fail. What we need to do is to add a command to run wemake-python-styleguide and make sure this script exits with code 1 when the linting and checking fails.

To do so, simply add the following code before the code checking for whitespace errors.

# Your added code to run wemake-python-styleguide. Add this before
# the whitespace error lines
flake8 .
if [ $? -ne 0 ]
then
echo "Please fix the ERRORS and commit again."
exit 1
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

Save the file and rename it from pre-commit.sample to pre-commit. Test it by creating a simple python file dummy.py with only one line of code

print("Hello world")

and try to commit it. You shall get these outputs:

./dummy.py:1:1: C101 Coding magic comment not found
./dummy.py:1:1: C0304 Final newline missing
./dummy.py:1:1: C0114 Missing module docstring
./dummy.py:1:21: W292 no newline at end of file
Please fix the ERRORS and commit again.

To learn what each error code stands for and how they can be corrected, refer to https://wemake-python-stylegui.de/en/latest/pages/usage/violations/index.html

Final notes

1. wemake-python-styleguide is VERY strict and opinionated (as they admit themselves), but it indeed is a very powerful tool, so use it wisely.

2. Keep in mind that wemake-python-styleguide is a plugin of flake8, so you can add a .flake8 configuration file to determine what rule to ignore and what file/folder to exclude (e.g. please exclude your venv folder, you will thank me later).

3. For legacy code, wemake-python-styleguide supports a tool called flakehell to ignore errors from legacy code and start enforcing rules on newly added code. However, from my test, flakehell only runs regular version of flake8, not the steroid-juiced wemake-python-styleguide.

CORRECTION

When I wrote the content above, I was not aware thatpre-commit is able to run local hooks (thanks to this awesome guide). Now that I know local hooks are available, all we need to do is to ask pre-commit to run our local flake8 , which is installed via wemake-python-styleguide, instead of the default one. The .pre-commit-config.yaml file looks like this to run local flake8 :

repos:
- repo: local
hooks:
- id: flake8
name: flake8
description: Python style guide enforcement
entry: flake8
args: ["--config=setup.cfg"]
language: python
types: [python]

Hi, I am from the Earth. And you?

Hi, I am from the Earth. And you?