Skip to content

Gradually introduce mypy type checking #46

Description

@subindevs

Follow-up to #42.

Issue #42 set up ruff (lint + format) with pre-commit and CI, but mypy was dropped from that issue's scope due to the size of the gap between the codebase and a clean mypy run.

Current state

With a starting config of:

[tool.mypy]
python_version = "3.10"
disallow_untyped_defs = true
ignore_missing_imports = true
explicit_package_bases = true
mypy_path = "."

mypy . reports 3002 errors across 225 of 299 source files. Breakdown by error code:

Code Count Notes
no-untyped-def 1928 missing type annotations — dominant category
assignment 401 often implicit-Optional defaults (def f(x: str = None))
union-attr 267 .attr access on X | None
attr-defined 238 some may be real bugs worth investigating
var-annotated 48
arg-type 40
return-value 28
index, operator, misc, dict-item, etc. ~36

Note: explicit_package_bases = true + mypy_path = "." is required to resolve scripts/projection_explorer.py (imported as both scripts.projection_explorer and projection_explorer — no __init__.py in scripts/).

Proposed approach: incremental adoption

Fixing ~3000 errors in one pass isn't practical. Instead:

  1. Add the [tool.mypy] config above, but without disallow_untyped_defs initially (cuts ~1928 errors).
  2. Add [[tool.mypy.overrides]] blocks with ignore_errors = true for the modules/packages that still fail, enumerated explicitly (e.g. gently.harness.bridge, benchmarks.*, etc.) — captured as a checklist below.
  3. Wire mypy . into CI (.github/workflows/lint.yml) and the mypy pre-commit hook — it passes immediately because failing modules are excluded.
  4. New modules and PRs touching excluded modules are expected to clean up that module's overrides entry and remove it from the ignore list as part of the change.
  5. Periodically (or via follow-up issues per subsystem), pick off modules from the ignore list — gently/harness/bridge.py alone accounts for ~25 errors and is a good first target given it's a single large file.

Tasks

  • Add lenient [tool.mypy] config (drop disallow_untyped_defs, keep ignore_missing_imports, explicit_package_bases, mypy_path)
  • Enumerate currently-failing modules and add [[tool.mypy.overrides]] with ignore_errors = true
  • Re-add mypy to .pre-commit-config.yaml and .github/workflows/lint.yml
  • Re-add mypy to the dev dependency group in pyproject.toml
  • Update CONTRIBUTING.md to document the incremental-typing policy
  • (Stretch) Fix gently/harness/bridge.py (~25 errors, mostly implicit-Optional and missing return types) as a first incremental cleanup

Acceptance Criteria

  • mypy . passes in CI and pre-commit (via the override list)
  • The policy for adding new code / shrinking the ignore list is documented in CONTRIBUTING.md

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions