-
Notifications
You must be signed in to change notification settings - Fork 46
Add a lazy_import utility function (part one of our lazy import strategy) #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 introduces a new utility function lazy_import to enable lazy importing of modules and their attributes, aiming to reduce startup overhead and prevent unnecessary dependencies.
- Adds the lazy_import function in redisvl/utils/utils.py with a proxy class for delayed module import.
- Adds comprehensive unit tests in tests/unit/test_utils.py to verify lazy importing behavior for standard modules, submodules, functions, and error scenarios.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
tests/unit/test_utils.py | Added tests covering lazy import functionality including favorable error handling and edge cases. |
redisvl/utils/utils.py | Implemented the lazy_import function using a proxy class to delay actual import until usage. |
return getattr(self._module, name) | ||
|
||
# If the attribute doesn't exist, raise AttributeError | ||
raise AttributeError(f"{self._module_path} has no attribute '{name}'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message for a missing attribute does not include the term "module" as expected by the tests. Consider changing it to "module '{self._parts[0]}' has no attribute '{name}'" to provide a clearer and more consistent error message.
raise AttributeError(f"{self._module_path} has no attribute '{name}'") | |
raise AttributeError(f"module '{self._parts[0]}' has no attribute '{name}'") |
Copilot uses AI. Check for mistakes.
Add a new
lazy_import
utility function that enables lazy importing of modules and objects. This helps reduce startup time and avoid unnecessary dependencies by only importing modules when they are actually needed.Key features:
Implementation
Uses a proxy class that defers the actual import until the module or attribute is accessed.
Examples
Basic usage with standard library modules
Importing specific functions or attributes
Working with third-party libraries
Error handling
Performance benefits