Setting Up Automatic Linting and Type Checking (Python, GHA)
I work with open-source & private Python projects on GitHub on a daily basis.
I use Github Actions to run my tests, linting, static type checking, and other CI/CD tasks.
In this short guide I wanted to share my go-to tools and configurations for setting up my projects.
Here, I will mostly focus on setting up linting with ruff and static type checking with mypy.
The main goal is to be able to run both ruff and mypy on every pull request, and every push to the main branch.
Additionally we would want to be able to run these tools locally, with the same configuration so that we get consistent results locally & remote.
Step 1 - Install dependencies
I personally love using pipenv, so in that case create/open a Pipfile in the root of your project and copy the following contents:
1 | [[source]] |
This basically specifies that our project has two dev dependencies of latest ruff & mypy.
In order to install these dependencies, simply run pipenv install --dev
.
Step 2 - Configure the tools
As one of our goals was to have consistent results between remote & local executions, I like having a configuration file for all of the tools, which means I like having a pyproject.toml
.
Create/open your pyproject.toml
file in the root of your project and copy the following contents:
1 | [tool.mypy] |
To read more about the configurations options, refer to the respective docs:
Step 3 - Setting up a remote workflow with GHA
After step 2, you should be able to run both ruff & mypy locally.
Now you will set an automatic workflow on GitHub, which will run ruff & mypy whenever a PR is opened and/or there was a push to the main branch.
Create a file linting_and_type_check.yaml
under .github/workflows
and copy the following contents:
1 | name: lint_and_type_check |
If the above syntax is a bit mysterious, you can go through the syntax docs.
As soon as you will push the above file, you will see the actions being triggered.
Verify that both ruff and mypy executed successfully, and that they have consistent results with your local executions.
On GitHub, it should look like the following
Feel free to tweak this process with your own favorite tools, and if you have any questions you can reach out to me on twitter.
Looking for a powerful, self-hosted backend for forms?
I’m building Collecto — a production-ready tool designed to handle your forms with ease and security. Check it out here and be part of its journey!