Skip to content

refactor: Central engine registry with decorator-based registration#140

Merged
ericchansen merged 2 commits intomasterfrom
refactor/engine-registry
Mar 23, 2026
Merged

refactor: Central engine registry with decorator-based registration#140
ericchansen merged 2 commits intomasterfrom
refactor/engine-registry

Conversation

@ericchansen
Copy link
Owner

Summary

Adds a central engine registry (q2mm/backends/registry.py) that replaces scattered try/except backend detection with decorator-based registration and a single get_engine() / available_engines() API.

Changes

  • New: q2mm/backends/registry.pyregister_mm() / register_qm() decorators, lazy discovery via importlib, get_engine(name, **kwargs), available_engines(), and typed retrieval helpers
  • Engine classes: Added @register_mm("openmm") etc. to all 5 engine classes (OpenMM, Tinker, JAX, JAX-MD, Psi4)
  • test/conftest.py: Replaced 5 try/except blocks (34 lines) with a single available_engines() call + marker-to-registry mapping
  • q2mm/diagnostics/cli.py: Replaced _discover_backends() (45 lines of try/except) with a registry-based implementation (20 lines)
  • New: test/test_registry.py — 17 unit tests covering registration, availability, instantiation, kwargs forwarding, and error messages

Impact

  • Net -42 lines of duplicated backend detection code
  • Adding a new engine now requires only @register_mm("name") on the class — tests, CLI, and docs pick it up automatically
  • No changes to engine behavior or public API beyond the new registry module

Testing

  • All 438 tests pass (421 existing + 17 new), 83 skipped
  • Lint and format clean

Closes #134

- Create q2mm/backends/registry.py with register_mm/register_qm
  decorators, lazy discovery, get_engine/available_engines API
- Decorate all 5 engine classes (@register_mm or @register_qm)
- Replace 5 try/except blocks in conftest.py with registry query
- Replace _discover_backends() in diagnostics/cli.py with registry
- Add 17 unit tests for registry (test/test_registry.py)

Net -42 lines: eliminates duplicated backend detection in conftest
and cli.py. Adding a new engine now requires only the @register
decorator — tests, CLI, and docs get it automatically.

Closes #134

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 21:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a centralized backend engine registry (q2mm/backends/registry.py) to replace duplicated try/except backend detection across tests and diagnostics, enabling backend lookup via get_engine() and availability listing via available_engines().

Changes:

  • Added q2mm/backends/registry.py with decorator-based registration (register_mm / register_qm) and discovery + retrieval APIs.
  • Updated all engine classes to self-register via decorators (OpenMM, Tinker, JAX, JAX-MD, Psi4).
  • Refactored backend detection in test/conftest.py and q2mm/diagnostics/cli.py to use the registry; added test/test_registry.py coverage.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
q2mm/backends/registry.py New central registry, discovery, availability checks, and engine construction APIs.
q2mm/backends/mm/openmm.py Registers OpenMM engine via @register_mm("openmm").
q2mm/backends/mm/tinker.py Registers Tinker engine via @register_mm("tinker").
q2mm/backends/mm/jax_engine.py Registers JAX engine via @register_mm("jax").
q2mm/backends/mm/jax_md_engine.py Registers JAX-MD engine via @register_mm("jax-md").
q2mm/backends/qm/psi4.py Registers Psi4 engine via @register_qm("psi4").
test/conftest.py Replaces per-backend try/except detection with registry-driven availability + marker mapping.
q2mm/diagnostics/cli.py Replaces manual backend discovery with registry-based discovery for benchmark CLI.
test/test_registry.py Adds unit tests for registration, availability, instantiation, kwargs forwarding, and errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Gate OpenMM-dependent registry tests with @pytest.mark.openmm
  so they skip when OpenMM is not installed
- Fix EngineNotAvailable docstring: 'not registered' (not 'not
  available') to match actual semantics
- Make error message assertion backend-agnostic

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ericchansen ericchansen merged commit 559de96 into master Mar 23, 2026
11 checks passed
@ericchansen ericchansen deleted the refactor/engine-registry branch March 23, 2026 21:29
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.

refactor: Engine registry with decorator-based registration

2 participants