Skip to content

Latest commit

 

History

History
167 lines (121 loc) · 3.43 KB

File metadata and controls

167 lines (121 loc) · 3.43 KB

forge-ci-python-tools-mypy

Run mypy static type checking for Python code.

Usage

jobs:
  mypy:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-mypy.yml@v1
    with:
      dependencies-cache-key: ${{ needs.dependencies.outputs.dependencies-cache-key }}

Inputs

Input Type Required Default Description
python-version string No '3.12' Python version to use
working-directory string No '.' Working directory for type checking
dependencies-cache-key string No '' Cache key from dependency-manager job
mypy-config string No '' Path to custom mypy config file
runs-on string No 'ubuntu-latest' Runner to use

Outputs

None

Behavior

  1. Restore Cache: Restores dependencies if cache key provided
  2. Add to PATH: Adds .venv/bin to PATH for mypy access
  3. Run mypy: Executes type checking with optional custom config

Examples

With Cached Dependencies

jobs:
  dependencies:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-dependency-manager.yml@v1

  mypy:
    needs: dependencies
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-mypy.yml@v1
    with:
      dependencies-cache-key: ${{ needs.dependencies.outputs.dependencies-cache-key }}

With Custom Config

jobs:
  mypy:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-mypy.yml@v1
    with:
      mypy-config: 'config/mypy.ini'

Standalone (No Cache)

jobs:
  mypy:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-mypy.yml@v1

Configuration

mypy configuration in pyproject.toml:

[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false

Or separate mypy.ini:

[mypy]
python_version = 3.12
warn_return_any = True
warn_unused_configs = True
disallow_untyped_defs = True

[mypy-tests.*]
disallow_untyped_defs = False

Or .mypy.ini in project root:

[mypy]
files = src/
exclude = tests/
strict = True

Troubleshooting

mypy Not Found

Problem: mypy: command not found

Cause: mypy not installed in dependencies.

Solution: Add mypy to dev dependencies:

# Poetry
poetry add --group dev mypy

# uv
uv add --dev mypy

# venv
echo "mypy>=1.0.0" >> requirements-dev.txt

Type Stub Errors

Problem: "Cannot find implementation or library stub for module"

Cause: Missing type stubs for third-party libraries.

Solution: Install type stubs:

# Poetry
poetry add --group dev types-requests types-pyyaml

# uv
uv add --dev types-requests types-pyyaml

# venv
pip install types-requests types-pyyaml

Strict Mode Failures

Problem: Too many type errors in existing codebase.

Cause: Strict type checking enabled for legacy code.

Solution: Gradual type checking adoption:

[tool.mypy]
# Start lenient
warn_return_any = false
disallow_untyped_defs = false

# Gradually enable stricter checks
warn_unused_ignores = true
warn_redundant_casts = true

See Also