How to use Black, Pylint and Mypy in GitHub Actions?

python

Python’s not the strictest language, so to have any confidence in your code you need to hit it with a barrage of checks to ensure it meets at least some level of quality.

I use the following code quality checks:

The following Github Actions workflow will check your code when a Pull Request is created, catching problems before they’re merged.

How to add the Github Actions workflow

Add the following to your repository in .github/workflows/code-quality.yml.

name: Checks
on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    name: Checks
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
      with:
        python-version: 3.x
    - run: pip install --upgrade pip
    - run: pip install "black<23" pylint==v3.0.0a3 mypy==v0.902
    - run: black --diff --check $(git ls-files '*.py')
    - run: pylint --disable=all --enable=unused-import $(git ls-files '*.py')
    - run: mypy --strict $(git ls-files '*.py')

Notes

See also: How to use Black, Pylint and Mypy in Pre-commit?