On the previous blog post, I’ve introduced how to configure the static code analysis
flake8 to make the same code style and reduce the potential bugs.
In this blog post, I will show you how to use
pre-commit to execute the flake8 when you commit the source code to Git.
- pre-commit: https://pre-commit.com/
To execute the flake8 automatically by pre-commit, we need to install the pre-commit. Execute the command below to install the pre-commit.
pip install pre-commit
After installing, don’t forget to store it to
pip freeze > requirements.txt
Done! we’ve seen how to install the pre-commit.
To use the pre-commit to execute the flake8 automatically, we need to create the configuratoin file of the pre-commit. Execute the command below to create a configuration file of the pre-commit.
pre-commit sample-config > .pre-commit-config.yaml
When you open the created
.pre-commit-config.yaml file, you can see the contents like below.
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
We can see the sample of the configuration file provided by the pre-commit.
The pre-commit configuration file
.pre-commit-config.yaml has the structure below basically.
- repo: repo-url
- id: hook-id
- repo: Repository URL from the pre-commit
- rev: Version of the feature
- id: Feature from pre-commit
The pre-commit provides some features. You can see them on the link below.
We’ll use the flake8. Open the
.pre-commit-config.yaml file and modify it like below.
- repo: https://gitlab.com/pycqa/flake8
- id: flake8
I’ve configured the repo URL, version and flake8 hook of the pre-commit. You can see the configuration on the flake8 official documentation.
- flake8 hooks: https://flake8.pycqa.org/en/latest/user/using-hooks.html
To apply the configurations to Git Commit hook, execute the command below.
Done! we’ve ready to use the pre-commit to use the flake8
Let’s check the configuration we’ve set is working well. Execute the command below, that is executed based on
.pre-commit-config.yaml file, to start the pre-commit.
pre-commit run --all-files
You can see the result when you execute the command.
We’ve checked the pre-commit, that we’ve configured, is working well. Commit the configurations to Git.
git add .
git commit -m 'Add pre-commit for flake8'
git push origin main
We’ve seen how to configure the pre-commit to execute the flake8 when we commit to Git. Note that if you clone the repository, don’t forget to execute
pre-commit install to register the pre-commit configuration to Git hooks.
git clone repository_url
# virtualenv venv
# source venv/bin/activate
pip install -r requirement.txt
So let’s do more productive development with automated flake8!
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
Deku created the applications with Flutter.
If you have interested, please try to download them for free.