Skip to content

refactor: defer heavy imports — blackjax/jax, sqlalchemy, IPython lazy (#1505) - #1506

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/lazy-heavy-imports
Aug 19, 2026
Merged

refactor: defer heavy imports — blackjax/jax, sqlalchemy, IPython lazy (#1505)#1506
Jammy2211 merged 1 commit into
mainfrom
feature/lazy-heavy-imports

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Defers PyAutoFit's heavy optional imports to first use, as part of the cross-repo import-time task (PyAutoFit#1505): import autolens drops from ~4.1s to ~2.1s, and import autofit alone from ~1.4s to ~0.8s, with blackjax/jax, sqlalchemy, and IPython no longer loading on bare import. Every workspace script and smoke-test process pays this cost, and smoke runs with JAX disabled — so the jax chain there was pure waste.

  • autofit/__init__.py: NSS, Aggregator, Query, GridSearchAggregator and the db module alias become lazy attributes (PEP 562 __getattr__ + __dir__), resolving identically on first access.
  • autofit/database/sqlalchemy_.py: sa/declarative become lazy proxies that import sqlalchemy on first attribute access (existing MockSQlAlchemy fallback preserved).
  • 14 search/settings modules move from autofit.database.sqlalchemy_ import sa under if TYPE_CHECKING with from __future__ import annotations (its only use is session: Optional[sa.orm.Session] signatures, which Py3.12 evaluates at def time).
  • paths/database.py defers its Fit/Info/sa imports to the methods that use them; result_builder.py moves its annotation-only Prior import under TYPE_CHECKING.
  • fitness.py imports IPython.display.clear_output at its single quick-update call site.

API Changes

None — internal changes only. af.NSS, af.Aggregator, af.Query, af.GridSearchAggregator and af.db resolve exactly as before (now on first access instead of at import); dir(autofit) still lists them. No symbols removed, renamed, or re-signatured.
See full details below.

Test Plan

  • Full suite: 2005 passed, 16 skipped (pre-existing NSS skips — local blackjax 1.5)
  • python -X importtime -c "import autofit" shows no jax/blackjax/optax/IPython/sqlalchemy/numba
  • Lazy attrs resolve and construct; db.open_database("sqlite://") round-trip works
  • End-to-end Drawer model fit through the annotated search plumbing
  • Six-workspace smoke vs this branch: green except 4 pre-existing autolens_workspace_test jax_likelihood pin failures (identical on canonical main — control-tested; see issue)
Full API Changes (for automation & release notes)

Changed Behaviour

  • autofit.NSS, autofit.Aggregator, autofit.Query, autofit.GridSearchAggregator, autofit.db — now lazy module attributes (PEP 562); same objects on access, but from autofit import * (no __all__ was ever defined; no known users) would not bind them, and blackjax/sqlalchemy import errors surface at first access instead of at import autofit.
  • autofit.database.sqlalchemy_.sa / .declarative — lazy proxy objects delegating to the real sqlalchemy module on first attribute access (mock fallback unchanged when sqlalchemy is absent).

Migration

  • None required.

Generated by the PyAutoLabs agent workflow.

… use (#1505)

Lazy PEP 562 attributes for NSS/Aggregator/Query/GridSearchAggregator/db,
lazy sa proxy in database/sqlalchemy_.py, TYPE_CHECKING sa imports with
future annotations in 14 modules, deferred Fit/Info in paths/database.py,
function-local IPython import in fitness.py. import autofit 1.4s -> 0.8s.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Aug 19, 2026
@Jammy2211
Jammy2211 merged commit 752f570 into main Aug 19, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/lazy-heavy-imports branch August 19, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-release PR queued for the next release build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant