fix: lazy import utilities#814
Conversation
676d9c8 to
a7b351f
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes and strengthens the library’s lazy-import behavior by preventing unintended imports triggered by bec_lib.utils package initialization and by making lazy_import_from more ergonomic/consistent.
Changes:
- Updated
lazy_import_fromto accept a single string name, return tuples for multi-name imports, and materialize proxies only once. - Switched
bec_lib.utilsre-exports to a PEP 562__getattr__-based lazy export mechanism to avoid importing heavier utilities at import time. - Reworked tests to validate lazy import semantics and verify that importing
bec_lib.utils.import_utilsdoes not importscan_utils.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
bec_lib/tests/test_import_utils.py |
Replaced prior tests with new coverage focused on lazy import behavior and import side effects. |
bec_lib/bec_lib/utils/import_utils.py |
Improved typing/behavior of lazy_import_from; moved inspect import inside isinstance_based_on_class_name. |
bec_lib/bec_lib/utils/__init__.py |
Implemented lazy re-exports via module __getattr__ to prevent eager imports of scan_utils/others. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
a7b351f to
26afc86
Compare
It is not like I don't agree with the changes but just out of curiosity: What exactly made it heavy in these imports? I would have thought functools should be quite fast |
import_utils previously triggered bec_lib.utils.init, which imported scan_utils, and scan_utils imports typeguard |
26afc86 to
819a27e
Compare
Description
This PR fixes
bec_lib.utils.import_utils.lazy_import_from()and makes thebec_lib.utilspackage import path lightweight.lazy_import_from()now correctly supports both a single string and a sequence of strings. The previous implementation treated plain strings as iterables of characters and returned a one-shot generator for multi-name imports. The new implementation returns a single proxy for one name, a tuple of proxies for multiple names, and memoizes resolved attributes withinit_once=True.bec_lib.utils.__init__now exposes public utility helpers lazily. This is needed because Python importsbec_lib.utils.__init__beforebec_lib.utils.import_utils, so the previous eager imports ofscan_utils,rpc_utils, andthreading_utilsmade importing the lazy import helper unnecessarily heavy.Type of Change
lazy_import_from()string handling, multi-name return behavior, and proxy memoization.bec_lib.utilspublic exports lazy to avoid eager imports when importingbec_lib.utils.import_utils.