Skip to content

Modernize static typing, ship py.typed, and replace typeguard with ty + strict mypy - #515

Closed
basnijholt wants to merge 10 commits into
mainfrom
modernize-typing
Closed

basnijholt wants to merge 10 commits into
mainfrom
modernize-typing

Conversation

@basnijholt

Copy link
Copy Markdown
Member

Summary

I modernized the type annotations across adaptive, replaced runtime typeguard instrumentation with static type checking (ty and strict mypy) in CI, and added a PEP 561 adaptive/py.typed marker so downstream users benefit from the annotations.

Key changes

  • Drop typeguard and add static type checking in CI: removed .github/workflows/typeguard.yml, pytest_typeguard from noxfile.py, the typeguard test dependency, and the adaptive/_types.py workaround; added .github/workflows/typecheck.yml running pinned ty check --error-on-warning and mypy adaptive against installed scientific dependencies, and scoped the pre-commit mypy hook to adaptive/.
  • Ship PEP 561 py.typed: added adaptive/py.typed and configured [tool.setuptools.package-data] so built wheels include it.
  • Simplify internal types and preserve concrete learner types:
    • Replaced internal uses of adaptive.types (Float, Int, Bool, Real) with standard Python numeric and numpy.typing annotations while keeping adaptive/types.py as a backwards-compatible shim (still accessible via import adaptive; adaptive.types.Real).
    • Replaced the brittle XsType0/1/N and YsType0/1/N aliases in learner1D.py with cleaner Sequence/loss-function types and moved the small sorted-dict typing shim to adaptive/learner/_sorted_types.py.
    • Made BaseRunner, Runner, BlockingRunner, AsyncRunner, DataSaver, and BalancingLearner generic over their wrapped learner type (Generic[LearnerType]).
    • Added full type annotations to LearnerND, triangulation.py, and triangulation_backend.py (including a shared TriangulationProtocol covering the Python and Rust backends), as well as BaseLearner, utils.py, notebook_integration.py, and the remaining learners.
    • Fixed Learner2D.loss() to handle list-valued loss_per_triangle results (such as triangle_loss) via np.max(losses) instead of calling .max().
    • Preserved Python 3.11+, runtime signature, typing.get_type_hints(), and pickle compatibility.
  • Add typing tests: added adaptive/tests/test_typing.py and tests/typing_contract.py covering py.typed, legacy adaptive.types imports, generic subscriptions, get_type_hints(), and public API static contracts (with and without optional Rust/pandas dependencies).

Supersedes #377 and #408.

Verification

  • ruff check . and ruff format --check . passed.
  • ty check --error-on-warning passed.
  • mypy adaptive passed both with full CI dependencies and in the isolated pre-commit environment (pre-commit run mypy --all-files).
  • Downstream mypy and ty checks against an installed wheel passed (both with pure Python and with adaptive-triangulation==0.3.1), including async callbacks, list/NumPy bounds, custom loss callbacks, fixed-length LearnerND callbacks, HoloViews Overlay live plots, synchronous ask -> learner.function -> tell, external BaseLearner subclasses, and .new() return types.
  • Full pytest suites passed on Python 3.11 (Python backend) and Python 3.13 (Rust backend), plus minimal-deps get_type_hints(), main-to-branch pickle compatibility, and Zensical docs build.

Preserve concrete learner types in runners and retire runtime typeguard instrumentation before adding static-only types.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Keep the base contract and concrete learners together so each commit type-checks. Preserve runtime learner signatures and pickle payloads.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Check installed scientific dependencies in CI and verify public generic subscriptions and the legacy numeric aliases.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Scope the mypy hook to the package, type learner constructors and new() honestly, and retain the legacy BaseLearner contract. Check downstream calls and annotation resolution without loading optional plotting libraries. Keep the downstream fixture outside the package and its runtime coverage measurements.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Accept sync and async callbacks without weakening the type of evaluated results passed to tell. Accept sequence and NumPy integrator bounds and keep the original container in pickle state.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Cover NumPy bounds, notebook plotting, the optional Rust backend, and legacy imports in the typed public contract. Simplify local typing adapters and pin the CI checkers and stubs.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Type the selected backend as a concrete constructor returning the shared instance protocol. Keep Dask cache lookups on the original callback and retain the legacy types attribute without exporting it from wildcard imports.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Keep scalar constructor contracts without restricting calls to stored callbacks. Normalize only loss results that cannot be marked with negative infinity during point selection.

Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
Assisted-by: MindRoom DevAgent:openai/gpt-6.2-sol <https://github.com/mindroom-ai/mindroom>
@basnijholt

Copy link
Copy Markdown
Member Author

Closing in favor of #516, which achieves the same typing goals with a much leaner diff (1 cast, 0 private Protocols, single mypy checker, +12 net production lines).

@basnijholt basnijholt closed this Dec 6, 2026
basnijholt added a commit that referenced this pull request Dec 6, 2026
#516)

I cleaned up static typing across `adaptive`, replaced runtime `typeguard` instrumentation with pinned `mypy==2.4.1` in CI, and shipped PEP 561 `adaptive/py.typed`.

Key changes:
- Drop `.github/workflows/typeguard.yml`, `pytest_typeguard` from `noxfile.py`, the `typeguard` test dependency, and `adaptive/_types.py`; add `.github/workflows/typecheck.yml` and deterministic `[tool.mypy]` settings (including `follow_imports = "skip"` overrides for `adaptive_triangulation.*`, `scipy.*`, and `pandas.*`).
- Ship `adaptive/py.typed` via `[tool.setuptools.package-data]`.
- Remove internal `adaptive.types` usage and legacy `XsType*`/`YsType*` aliases in `learner1D.py` while keeping `adaptive/types.py` as a backwards-compatible shim.
- Make `BaseRunner`, `Runner`, `BlockingRunner`, `AsyncRunner`, `DataSaver`, and `BalancingLearner` generic (`Generic[LearnerType]`), widen `BalancingLearner` learner input to `Sequence[LearnerType]`, and add clean public-API annotations to `LearnerND` and `BaseLearner`.
- Fix small edge cases (`tell_many` iterators, list-valued `Learner2D.loss()`, neighbor-aware 1-element array losses in `LearnerND`, iterable `cdims`/`combos` in `BalancingLearner`) and add focused tests in `adaptive/tests/test_typing.py` and `adaptive/tests/test_learner2d.py`.

Supersedes #515, #377, and #408.

Assisted-by: MindRoom DevAgent (orchestrator: google/gemini-4-neon; implementer: openai/gpt-6.2-sol; reviewers: anthropic/claude-opus-5.5, openai/gpt-6.2-astra, openai/gpt-6.2-sol, anthropic/claude-fable-5.2) <https://github.com/mindroom-ai/mindroom>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant