Skip to content

Clear the last pyright errors and fix the Laplace dtmax reactor rebuild - #1109

Queued
ctdegroot wants to merge 2 commits into
mainfrom
claude/pyright-remaining
Queued

ctdegroot wants to merge 2 commits into
mainfrom
claude/pyright-remaining

Conversation

@ctdegroot

Copy link
Copy Markdown
Member

Finishes the pyright reduction: pyright aquakin reports no errors, and the scripts/check_types.py ratchet now records none, so any new error fails it.

Summary

A bug found on the way, fixed here. calibrate(reactor, ..., laplace=LaplaceConfig(dtmax=...)) failed with a TypeError for a BiofilmReactor. The tighter-capped reactor for the Laplace Hessian was rebuilt as type(reactor)(model, conditions, settings=...), which drops a biofilm reactor's required geometry (n_layers, thickness, ...). Pyright flagged that call because ConditionedReactor is a Protocol with no constructor. The fix is a new with_dtmax(reactor, dtmax) beside with_adjoint in integrate/_config.py. Both share one _clone_reactor helper: a shallow copy with the attributes set and empty compile caches. The copy also keeps an adjoint that calibrate swapped in after construction. For a batch reactor, the only one this path worked for before, the copy carries the same settings and adjoint as the rebuild did.

The typing fixes, by pattern:

  • AST nodes (core/nodes.py, parser.py, registry.py, vector_kernel.py):
    • ASTNode declares __dataclass_fields__ / __dataclass_params__ as annotations with no value. Pyright now sees the dataclass shape every concrete node has, while is_dataclass stays false on an undecorated subclass, which register_rate_function still refuses.
    • RateFunction declares FUNCTION_NAME the same way.
    • The kernel table walks RateFunction.__subclasses__(): every operator node is one, and the four leaves have no KIND. The table is identical to main's.
  • Probes replaced by Protocols:
    • scenarios._kpis_of now checks a _KpiReport Protocol.
    • The units check reads a rate function's DIMENSION through a _DeclaresDimension Protocol.
    • Both probe-ratchet entries go down.
  • Redeclared closures:
    • make_chemistry_rhs defines one rhs that applies rate_scale when given.
    • The recycle refinement defines one G that branches on resolve_T.
    • Both branch on a Python value, so the traced program is the same.
  • Annotations:
    • build_bsm2(asm1_model, adm1_model) take CompiledModel | None and temperature_model takes TemperatureModel | None, as their docstrings already said.
    • _dispersion is ArrayLike, and a cfd cache is typed Callable.
    • np.all(..., axis=1) replaces .all(axis=1), which the stubs type as a scalar.
    • The derived precipitation stoichiometry is passed as dict(stoich).
    • An assert documents that exactly one dispersion argument is given.
  • Third-party stubs: two # pyright: ignore[reportArgumentType] with the reason on the line.
    • diffrax annotates VeryChord(atol=) as float, but its check broadcasts a per-species array.
    • scipy's stubs list only the string choices for least_squares(jac=).
  • Catalog, IFAS, spec (free once Name unit settings in words, fold advanced ones away, and move a model in one step #1091 landed):
    • _settings_of takes a type.
    • unit_ports asserts the built object is a Unit before it reads the ports; an influent has already returned by then.
    • The IFAS unit passes n_locations=1 (the default) explicitly, so the conditions dict cannot fill it.
    • The YAML 1.2 bool constructor is a typed function, not a lambda.
  • Lexer: located() sets position on any language's exception class through vars(exc), since most of those classes declare no such attribute.

Things a reviewer should know

  • with_dtmax is exported from integrate/_common.py, and the pinned export list in tests/unit/test_common_exports.py gains it.
  • I did not give ConditionedReactor an __init__. The reactors' constructors differ (a PFR takes n_points, length and velocity), so a Protocol constructor would be untrue.
  • The vars(exc)["position"] write avoids setattr with a constant, which ruff's B010 refuses. The other typed option was a shared LocatedError base class for every expression language. That changes those exceptions' class hierarchy, and tokenize's default ValueError would need replacing, so I left it.
  • Whether to make pyright a required CI check, now that it is at zero, is the owner's call. This PR does not change CI.
  • full-ci: the change touches integrate/_solver.py, plant/recycle.py and the calibration modules.

Testing

  • Regression test test_laplace_dtmax_keeps_a_biofilm_reactors_geometry (fast tier, about 10 s). On main's problem.py it fails with TypeError: BiofilmReactor.__init__() missing 5 required keyword-only arguments; with the fix it passes.
  • Unit test test_with_dtmax_keeps_what_the_reactor_was_built_with checks four things:
    • the copy has the cap;
    • the copy keeps the layers and the adjoint;
    • the original is untouched;
    • the copy's caches are fresh.
  • Bit-for-bit against main (PYTHONPATH set to this checkout, both runs confirmed to import it):
    • the BSM2 RHS and a JVP with recycle_tol set, with and without a temperature-carrying influent (the two G branches);
    • the ASM1 chemistry RHS and Jacobian with and without rate_scale;
    • the kernel table.
  • Fast tier with coverage, on the branch rebased onto current main: 5,835 passed, 1 skipped, 0 failed. I ran it in ten chunks with --cov-append, because a run in the background was lost to two container restarts.
  • ruff check, ruff format --check and scripts/check_types.py are clean, and pyright aquakin reports 0 errors.
  • Strict docs build (sphinx-build -W --keep-going, offline): clean.
  • Not run locally: the slow and validation suites (full-ci runs them).

Patch coverage

python scripts/patch_coverage.py: 100.00% of 52 changed lines.

Rule proposals

  • What went wrong: _ReactorForwardModel.with_dtmax rebuilt a reactor from (model, conditions, settings). That fails for any reactor whose constructor takes more, and it silently resets anything set after construction.
  • A check instead of prose? Pyright already reports this call. The ratchet records it as debt, though, so nothing flagged it as a bug.
  • Proposed text for .claude/rules/integration.md (it loads for aquakin/integrate/**, where the rebuild was written): "Make a variant of an existing reactor (another adjoint, another step cap) with with_adjoint / with_dtmax, a copy. Do not rebuild it from (model, conditions, settings): the reactor constructors take different arguments, and a rebuild drops what was set after construction."
  • What it replaces: nothing.

Generated by Claude Code

calibrate(..., laplace=LaplaceConfig(dtmax=...)) rebuilt the reactor from
(model, conditions, settings), which fails for a BiofilmReactor whose
geometry is required. It now takes a with_dtmax copy, which shares
with_adjoint's clone helper and keeps the geometry and any swapped adjoint.

The typing fixes: ASTNode declares its dataclass shape without making it
a dataclass; RateFunction declares FUNCTION_NAME; the kernel table walks
RateFunction's subclasses; Protocols replace the kpis() and DIMENSION
probes; one rhs and one recycle G instead of two redeclared closures;
annotations for build_bsm2, the dispersion and a cfd cache; reasoned
ignores for the diffrax atol and scipy jac stub mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_settings_of takes a class; unit_ports asserts the built object is a Unit
before reading its ports; the IFAS unit passes n_locations=1 explicitly
instead of letting a condition dict fill it; the YAML 1.2 bool constructor
is a typed function. pyright now reports no errors over aquakin, and the
check_types ratchet records none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ctdegroot ctdegroot added the full-ci Run the full slow+validation suite on this PR (opt-in pre-merge check) label Dec 4, 2026 — with Claude
@ctdegroot
ctdegroot added this pull request to the merge queue Dec 4, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full slow+validation suite on this PR (opt-in pre-merge check)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants