From 32f9f92042ab530220ac9d159045c91d311affa7 Mon Sep 17 00:00:00 2001 From: Sara Robinson Date: Wed, 28 Jan 2026 07:27:27 -0800 Subject: [PATCH] chore: Add GitHub Action check to run mypy Merge https://github.com/google/adk-python/pull/4276 This adds 2 mypy GitHub Actions checks: * `mypy.yml` runs mypy on every PR. This will list all errors, so it shouldn't block until errors are resolved. * `mypy-new-errors.yml` compares the PR branch to `main` to ensure no new mypy errors are introduced. This check can be made blocking now and can be removed once all errors are resolved in favor of `mypy.yml`. ### Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document. - [x] I have performed a self-review of my own code. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have added tests that prove my fix is effective or that my feature works. - [x] New and existing unit tests pass locally with my changes. - [x] I have manually tested my changes end-to-end. - [x] Any dependent changes have been merged and published in downstream modules. COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/4276 from sararob:mypy-gh-actions 63130773c4862e90e352d4cb79221d78a6b9f5fd PiperOrigin-RevId: 862226296 --- .github/workflows/mypy-new-errors.yml | 74 +++++++++++++++++++++++++++ .github/workflows/mypy.yml | 32 ++++++++++++ pyproject.toml | 4 +- 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/mypy-new-errors.yml create mode 100644 .github/workflows/mypy.yml diff --git a/.github/workflows/mypy-new-errors.yml b/.github/workflows/mypy-new-errors.yml new file mode 100644 index 0000000000..85dd0073c1 --- /dev/null +++ b/.github/workflows/mypy-new-errors.yml @@ -0,0 +1,74 @@ +name: Mypy New Error Check + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + + +jobs: + mypy-diff: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.10', '3.11', '3.12', '3.13',] + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install uv + uses: astral-sh/setup-uv@v5 + + - name: Generate Baseline (Main) + run: | + # Switch to main branch to generate baseline + git checkout origin/main + + git checkout ${{ github.sha }} -- pyproject.toml + + # Install dependencies for main + uv venv .venv + source .venv/bin/activate + uv sync --all-extras + + # Run mypy, filter for errors only, remove line numbers (file:123: -> file::), and sort + # We ignore exit code (|| true) because we expect errors on main + uv run mypy . | grep "error:" | sed 's/:\([0-9]\+\):/::/g' | sort > main_errors.txt || true + + echo "Found $(wc -l < main_errors.txt) errors on main." + + - name: Check PR Branch + run: | + # Switch back to the PR commit + git checkout ${{ github.sha }} + + # Re-sync dependencies in case the PR changed them + source .venv/bin/activate + uv sync --all-extras + + # Run mypy on PR code, apply same processing + uv run mypy . | grep "error:" | sed 's/:\([0-9]\+\):/::/g' | sort > pr_errors.txt || true + + echo "Found $(wc -l < pr_errors.txt) errors on PR branch." + + - name: Compare and Fail on New Errors + run: | + # 'comm -13' suppresses unique lines in file1 (main) and common lines, + # leaving only lines unique to file2 (PR) -> The new errors. + comm -13 main_errors.txt pr_errors.txt > new_errors.txt + + if [ -s new_errors.txt ]; then + echo "::error::The following NEW mypy errors were introduced:" + cat new_errors.txt + exit 1 + else + echo "Great job! No new mypy errors introduced." + fi diff --git a/.github/workflows/mypy.yml b/.github/workflows/mypy.yml new file mode 100644 index 0000000000..085ab68e93 --- /dev/null +++ b/.github/workflows/mypy.yml @@ -0,0 +1,32 @@ +name: Mypy Type Check + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + mypy: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.10', '3.11', '3.12', '3.13',] + + steps: + - uses: actions/checkout@v4 + + - name: Install uv + uses: astral-sh/setup-uv@v1 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: uv sync --all-extras + + - name: Run mypy + + run: uv run mypy . --strict diff --git a/pyproject.toml b/pyproject.toml index 08511a8dcb..c09546451a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -216,9 +216,9 @@ asyncio_mode = "auto" [tool.mypy] python_version = "3.10" -exclude = "tests/" +exclude = ["tests/", "contributing/samples/"] plugins = ["pydantic.mypy"] # Start with non-strict mode, and swtich to strict mode later. -# strict = true +strict = true disable_error_code = ["import-not-found", "import-untyped", "unused-ignore"] follow_imports = "skip"