Mypy GitLab Code Quality
mypy-gitlab-code-quality is a simple Python script designed to generate a GitLab Code Quality report from the output of Mypy, the static type checker for Python. The current version is 1.3.0, released on March 31, 2025. It appears to have a regular, though not rapid, release cadence, primarily updating to ensure compatibility with Mypy and GitLab's Code Quality specifications.
Warnings
- gotcha While `mypy-gitlab-code-quality` supports both JSON and plain text output from `mypy`, JSON output is strongly recommended for more robust and consistent parsing. Relying on plain text output might lead to unexpected behavior if `mypy`'s output format changes in future versions.
- gotcha GitLab Code Quality reports expect file paths to be relative to the project root. If the `mypy` output contains absolute paths or paths that are not correctly relative to the project's root directory, code quality issues might not be displayed or linked correctly in GitLab merge requests.
- gotcha The `mypy-gitlab-code-quality` script outputs the generated GitLab Code Quality JSON directly to `STDOUT`. For GitLab CI to properly collect and display this as a code quality artifact, the `STDOUT` must be redirected to a file (e.g., `codequality.json`). Failure to redirect will result in no report being generated or displayed by GitLab.
- gotcha GitLab recommends setting `allow_failure: true` for Code Quality jobs. This practice prevents the entire CI/CD pipeline from failing due to detected quality issues, allowing other pipeline stages (like tests) to complete. It also ensures a full overview of quality findings is available without blocking merge requests unnecessarily.
- deprecated The official GitLab Code Quality component based on CodeClimate scanning is deprecated and planned for removal in GitLab version 19.0 (around May 2026). While `mypy-gitlab-code-quality` is used by a separate, community-supported GitLab component (`codequality-os-scanners-integration`) for conversion to CodeClimate format, users should be aware of the broader deprecation of the built-in CodeClimate engine.
- gotcha The functionality of `mypy-gitlab-code-quality` depends on the output format of `mypy`. Major versions of `mypy` may introduce breaking changes to their output, drop support for older Python versions (e.g., Mypy 1.20 dropped Python 3.9 support), or change default behaviors (e.g., `--local-partial-types` in Mypy 2.0). Ensure compatibility between your installed `mypy` version and `mypy-gitlab-code-quality`, and your project's Python version.
Install
-
pip install mypy-gitlab-code-quality
Imports
- mypy-gitlab-code-quality
This library is primarily used as a command-line tool, typically invoked directly in shell scripts (e.g., in GitLab CI) rather than imported as a Python module for its core functionality.
Quickstart
image: python:alpine
codequality:
script:
- pip install mypy mypy-gitlab-code-quality
# Run mypy and capture its JSON output to a temporary file
- mypy program.py --output=json > mypy-out.json || true # '|| true' prevents job failure if mypy finds errors
# Process mypy output into GitLab Code Quality format and redirect to 'codequality.json'
- mypy-gitlab-code-quality < mypy-out.json > codequality.json
artifacts:
when: always
reports:
codequality: codequality.json
allow_failure: true # Recommended for code quality jobs to not block pipelines