refactor: defer heavy imports — blackjax/jax, sqlalchemy, IPython lazy (#1505) - #1506
Merged
Conversation
… 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Defers PyAutoFit's heavy optional imports to first use, as part of the cross-repo import-time task (PyAutoFit#1505):
import autolensdrops from ~4.1s to ~2.1s, andimport autofitalone 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,GridSearchAggregatorand thedbmodule alias become lazy attributes (PEP 562__getattr__+__dir__), resolving identically on first access.autofit/database/sqlalchemy_.py:sa/declarativebecome lazy proxies that import sqlalchemy on first attribute access (existingMockSQlAlchemyfallback preserved).from autofit.database.sqlalchemy_ import saunderif TYPE_CHECKINGwithfrom __future__ import annotations(its only use issession: Optional[sa.orm.Session]signatures, which Py3.12 evaluates at def time).paths/database.pydefers itsFit/Info/saimports to the methods that use them;result_builder.pymoves its annotation-onlyPriorimport under TYPE_CHECKING.fitness.pyimportsIPython.display.clear_outputat its single quick-update call site.API Changes
None — internal changes only.
af.NSS,af.Aggregator,af.Query,af.GridSearchAggregatorandaf.dbresolve 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
python -X importtime -c "import autofit"shows no jax/blackjax/optax/IPython/sqlalchemy/numbadb.open_database("sqlite://")round-trip worksDrawermodel fit through the annotated search plumbingmain— 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, butfrom 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 atimport autofit.autofit.database.sqlalchemy_.sa/.declarative— lazy proxy objects delegating to the realsqlalchemymodule on first attribute access (mock fallback unchanged when sqlalchemy is absent).Migration
Generated by the PyAutoLabs agent workflow.