This GitHub Action runs mypy via pre-commit on a ref range and reports diagnostics to pull
requests using reviewdog, using RDJSON as the intermediate format.
It uses the mypy-json-output hook to produce JSON lines, then converts them to RDJSON via a jq script.
Add the mypy hooks to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.19.0
hooks:
- id: mypy
additional_dependencies:
# add stubs the project needs
- types-requests
- types-setuptools
- id: mypy
alias: mypy-json-output
name: mypy (json output)
args:
[
"--output",
"json",
"--hide-error-context",
"--show-column-numbers",
"--show-absolute-path",
"--no-pretty",
"--ignore-missing-imports",
"--scripts-are-modules",
]
additional_dependencies:
# add stubs the project needs
- types-requests
- types-setuptools
stages: [manual]You also need:
- GitHub Actions enabled on the repository
secrets.GITHUB_TOKENavailable (default on GitHub-hosted runners)- A runner with
jqavailable - The jq filter file at
lib/mypy-json-to-rdjson.jqin this action repository actions/checkoutfetching enough history to include bothfrom-refandto-ref, for example:
- uses: actions/checkout@v4
with:
fetch-depth: 0| Name | Required | Description |
|---|---|---|
from-ref |
✅ | Base git ref (e.g. PR base SHA) |
to-ref |
✅ | Head git ref (e.g. PR head SHA) |
github-token |
✅ | GitHub token for reviewdog (secrets.GITHUB_TOKEN) |
| Name | Description |
|---|---|
exitcode |
Exit code returned by the mypy-json-output hook |
Example workflow for pull requests:
name: Type-check with mypy
on:
pull_request:
jobs:
mypy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run mypy via pre-commit + reviewdog
uses: leinardi/gha-pre-commit-mypy-reviewdog@v1
with:
from-ref: ${{ github.event.pull_request.base.sha }}
to-ref: ${{ github.event.pull_request.head.sha }}
github-token: ${{ secrets.GITHUB_TOKEN }}This will:
- Run
mypy-json-outputon the files changed betweenfrom-refandto-ref. - Convert the JSONL output to RDJSON using the provided jq script.
- Report diagnostics as inline comments on the pull request via reviewdog.
- Fail the job if type errors are found.
It’s recommended to pin to the major version:
uses: leinardi/gha-pre-commit-mypy-reviewdog@v1For fully reproducible behavior, pin to an exact tag:
uses: leinardi/gha-pre-commit-mypy-reviewdog@v1.0.0