Skip to content

fix: lazy import utilities#814

Open
wyzula-jan wants to merge 1 commit into
mainfrom
fix/lazy-import-utils
Open

fix: lazy import utilities#814
wyzula-jan wants to merge 1 commit into
mainfrom
fix/lazy-import-utils

Conversation

@wyzula-jan
Copy link
Copy Markdown
Contributor

@wyzula-jan wyzula-jan commented Apr 13, 2026

Description

This PR fixes bec_lib.utils.import_utils.lazy_import_from() and makes the bec_lib.utils package 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 with init_once=True.

bec_lib.utils.__init__ now exposes public utility helpers lazily. This is needed because Python imports bec_lib.utils.__init__ before bec_lib.utils.import_utils, so the previous eager imports of scan_utils, rpc_utils, and threading_utils made importing the lazy import helper unnecessarily heavy.

Type of Change

  • Fix lazy_import_from() string handling, multi-name return behavior, and proxy memoization.
  • Make bec_lib.utils public exports lazy to avoid eager imports when importing bec_lib.utils.import_utils.
  • Add regression tests for lazy import behavior and import boundaries.

@wyzula-jan wyzula-jan self-assigned this Apr 13, 2026
Copilot AI review requested due to automatic review settings April 13, 2026 13:40
@wyzula-jan wyzula-jan force-pushed the fix/lazy-import-utils branch from 676d9c8 to a7b351f Compare April 13, 2026 13:43
Copy link
Copy Markdown
Contributor

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 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_from to accept a single string name, return tuples for multi-name imports, and materialize proxies only once.
  • Switched bec_lib.utils re-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_utils does not import scan_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.

Comment thread bec_lib/tests/test_import_utils.py
Comment thread bec_lib/bec_lib/utils/import_utils.py
Comment thread bec_lib/bec_lib/utils/import_utils.py Outdated
Comment thread bec_lib/bec_lib/utils/__init__.py Outdated
@wyzula-jan wyzula-jan marked this pull request as draft April 13, 2026 13:45
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
bec_lib/bec_lib/utils/__init__.py 76.92% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@wyzula-jan wyzula-jan force-pushed the fix/lazy-import-utils branch from a7b351f to 26afc86 Compare April 13, 2026 13:57
@wyzula-jan wyzula-jan requested a review from a team April 13, 2026 14:12
@wyzula-jan wyzula-jan marked this pull request as ready for review April 13, 2026 14:12
@wakonig
Copy link
Copy Markdown
Member

wakonig commented Apr 13, 2026

bec_lib.utils.init now exposes public utility helpers lazily. This is needed because Python imports bec_lib.utils.init before bec_lib.utils.import_utils, so the previous eager imports of scan_utils, rpc_utils, and threading_utils made importing the lazy import helper unnecessarily heavy.

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

@wyzula-jan
Copy link
Copy Markdown
Contributor Author

bec_lib.utils.init now exposes public utility helpers lazily. This is needed because Python imports bec_lib.utils.init before bec_lib.utils.import_utils, so the previous eager imports of scan_utils, rpc_utils, and threading_utils made importing the lazy import helper unnecessarily heavy.

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

@wyzula-jan wyzula-jan force-pushed the fix/lazy-import-utils branch from 26afc86 to 819a27e Compare April 14, 2026 13:57
@wyzula-jan wyzula-jan enabled auto-merge (rebase) April 14, 2026 15:31
@wyzula-jan wyzula-jan disabled auto-merge April 14, 2026 15:52
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.

3 participants