Lazily re-export databricks_openai package root symbols#440
Open
mdbecker wants to merge 2 commits into
Open
Conversation
Author
|
@jennsun @annzhang-db @kunyuchen this might be helpful in preventing issues like #433 moving forward |
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
This PR preserves the existing
databricks_openaipackage-root API while resolving exported symbols lazily.The main motivation is to make narrow package-root imports like this avoid loading unrelated package-root re-exports:
DatabricksOpenAIis implemented indatabricks_openai.utils.clients. It does not require the MCP toolkit, vector search retriever tool, or Unity Catalog toolkit modules to be imported. Before this change, importingDatabricksOpenAIfrom the package root also eagerly imported those unrelated package-root re-exports because they were imported at package import time.After this change, each package-root export is imported only when that symbol is accessed. The public aliases in
__all__are unchanged.This PR also updates the existing package-root import smoke check so it is collected and reported as a normal pytest test item.
Changes
Replace eager package-root imports in
databricks_openai/__init__.pywith lazy re-exports using_LAZY_EXPORTSand module-level__getattr__.Preserve the existing public aliases in
__all__.Cache resolved aliases in module globals after first access.
Keep
__dir__aligned with module globals and__all__.Update the docstring alias list to include aliases that were already exported before this PR.
Wrap the existing package-root import smoke assertions in
test_imports.pyin atest_function so the smoke check is collected and reported as a normal pytest test item.Add focused lazy-import regression tests covering:
import databricks_openaidoes not eagerly import MCP/vector/UC integration modules.databricks_openai.DatabricksOpenAIloads the client module without importing unrelated integrations.References
__getattr__and__dir__, which is the mechanism used here for lazy package-root attribute access: https://peps.python.org/pep-0562/test_functions/methods fromtest_*.pyfiles. The existing package-root smoke check is now wrapped in atest_function so it appears as a collected and reportable test item: https://docs.pytest.org/en/stable/explanation/goodpractices.html#conventions-for-python-test-discoveryTesting
Validated with: