4 pre-commit Plugins to Automate Code Reviewing and Formatting in Python | by Khuyen Tran | Towards Data Science

When committing your Python code to Git, you need to make sure your code:
looks nice
is organized
conforms to the PEP 8 style guide
includes docstrings
However, it can be overwhelming to check all of these criteria before committing your code. Wouldn’t it be nice if you can automatically check and format your code every time you commit new code like below?
That is when pre-commit comes in handy. In this article, you will learn what pre-commit is and which plugins you can add to a pre-commit pipeline.
pre-commit is a framework that allows you to identify simple issues in your code before committing it.
You can add different plugins to your pre-commit pipeline. Once your files are committed, they will be checked by these plugins. Unless all checks pass, no code will be committed.
To install pre-commit, type:
pip install pre-commit
Cool! Now let’s add some useful plugins to our pre-commit pipeline.
black is a code formatter in Python.
To install black, type:
pip install black
Now to see what black can do, we’ll write a very long function like below. Since there are more than 79 characters in the first line of code, this violates PEP 8.
Let’s try to format the code using black:
$ black long_function.py
And the code is automatically formatted like below!
To add black to a pre-commit pipeline, create a file named .pre-commit-config.yaml and insert the following code to the file:
flake8 is a python tool that checks the style and quality of your Python code. It checks for various issues not covered by black.
To install flake8, type:
pip install flake8
To see what flake8 does, let’s write code that violates some guidelines in PEP 8.
Next, check the code using flake8:
$ flake8 flake_example.py
Aha! flake8 detects 3 PEP 8 formatting errors. We can use these errors as the guidelines to fix the code.
The code looks much better now!
To add flake8 to the pre-commit pipeline, insert the following code to the .pre-commit-config.yaml file:
isort is a Python library that automatically sorts imported libraries alphabetically and separates them into sections and types.
To install isort, type:
pip install isort
Let’s try to use isort to sort messy imports like below:
$ isort isort_example.py
Output:
Cool! The imports are much more organized now.
To add isort to the pre-commit pipeline, add the following code to the .pre-commit-config.yaml file:
interrogate checks your codebase for missing docstrings.
To install interrogate, type:
pip install interrogate
Sometimes, we might forget to write docstrings for classes and functions like below:
Instead of manually looking at all our functions and classes for missing docstrings, we can run interrogate instead:
$ interrogate -vv interrogate_example.py
Output:
Cool! From the terminal output, we know which files, classes, and functions don’t have docstrings. Since we know the locations of missing docstrings, adding them is easy.
$ interrogate -vv interrogate_example.py
The docstring for the __init__ method is missing, but it is not necessary. We can tell interrogate to ignore the __init__ method by adding -i to the argument:
$ interrogate -vv -i interrogate_example.py
Cool! To add interrogate to the pre-commit pipeline, insert the following code to the .pre-commit-config.yaml file:
The final code in your .pre-commit-config.yaml file should look like below:
To add pre-commit to git hooks, type:
$ pre-commit install
Output:
pre-commit installed at .git/hooks/pre-commit
Now we’re ready to commit the new code!
$ git commit -m 'add pre-commit examples'
And you should see something like below:
To prevent pre-commit from checking a certain commit, add --no-verify to git commit :
$ git commit -m 'add pre-commit examples' --no-verify
To choose which files to include and exclude when running black, create a file named pyproject.toml and add the following code to the pyproject.toml file:
To choose which errors to ignore or to edit other configurations, create a file named .flake8 and add the following code to the .flake8 file:
To edit interrogate’s default configurations, insert the following code to thepyproject.toml file:
Congratulations! You have just learned how to use pre-commit to automatically check and edit your code before committing it. I hope this article will make it effortless for you to review and format your code.
Your coworkers will also be happy when your code follows the common standards.
Feel free to play and fork the source code of this article here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Lj Miranda. (2018, June 20). Automate Python workflow using pre-commits: black and flake8. Lj Miranda. https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/.