This is a note about what I did to build a CI pipeline on GitHub action for Python static check.
Background
Recently, I’ve been working on an existing Python project. There are test codes with pytest
and some type checks with mypy
. However, there are missing Lints because the engineers use PyCharm and it automatically tweaks the codes based on its settings. I usually use Vim as my editor and write Ruby, JavasScript, and Python. So I decided to build GitHub action static checks like type and lint to develop the service safely and continuously.
What I did
First of all, I researched the popular lint library for Python and picked up Ruff. There are provided settings officially.
But I created the workflow file by myself because it’s not difficult to set up and I usually try to keep the control as much as possible. The following YAML file is the workflow content.
name: Lint
on: [push]
jobs:
ruff:
runs-on: ubuntu-latest
name: Lint with Ruff
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install Ruff
run: pip install ruff
- name: Run Ruff
run: ruff check --output-format=github src/
It works fine without the --output-format=github
option. I added the option as reading the documents
I also added mypy
check to the GitHub action like below.
name: Type check
on: [push]
jobs:
mypy:
runs-on: ubuntu-latest
name: Type check with mypy
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install Dependencies
run: |
pip install -r requirements.txt
- name: Run mypy
run: |
MYPYPATH=src mypy --disallow-untyped-defs --namespace-packages --explicit-package-bases src
I think there would be disagreement with installing the libraries. I found articles saying the imported libraries can be ignored using the ignore_missing_imports
option because they are out side of the service.
I can see their point but I checked some OSS project made by python and realized it’s quite common to have types in the OSS projects. I checked a few OSS randomly and all of them used the types. So I decided not to use ignore_missing_imports
option and added #type ignored
to the import line respectively if it doesn’t support type.
That’s it!