
Using tools like black, isort, and flake8 are super beneficial for a multitude of reasons. However, running these tools before opening a merge request can be a pain (that is, if you remember to run them).
This is where pre-commit hooks come into play. Git will invoke this file before you commit your code and run tools like linters, formatters, and security checks without having to run the tool yourself.
So, instead of doing something like:
$ isort .
$ black .
$ ruff check --fix .
# Now check your code in
$ git add .
$ git commit -m "fixed formatting issues"
You’d simply add the files and commit them:
$ git add .
$ git commit -m "fixed formatting issues"
Creating a Pre-commit Hook File
To create a pre-commit hook file, first install the appropriate library to help manage the file:
$ pip install pre-commit
Then, create a .pre-commit-config.yaml
file in the root of your repository with the sample found below. It’s recommended to check this into source control so that the file can be shared with the team.
Then, install your pre-commit hook:
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
Sample Pre-commit Hook File
# .pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
args: [--line-length=88]
language_version: python3.11
- repo: https://github.com/pre-commit/mirrors-isort
rev: ''
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://gitlab.com/pycqa/flake8
rev: '' # <- pin to flake8 tag/sha
hooks:
- id: flake8
args: ["--max-line-length=88"]
language_version: python3.11
- repo: https://github.com/pre-commit/mirrors-mypy
rev: '' # <- pin to mypy tag/sha
hooks:
- id: mypy
# Example: add extra deps so mypy can check third-party libs' stubs if needed:
additional_dependencies: []
args: ["--ignore-missing-imports"]
Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.