fix lazy import
This commit is contained in:
@@ -48,18 +48,25 @@ from .storage import (
|
|||||||
|
|
||||||
|
|
||||||
def lazy_external_import(module_name: str, class_name: str):
|
def lazy_external_import(module_name: str, class_name: str):
|
||||||
"""Lazily import an external module and return a class from it."""
|
"""Lazily import a class from an external module based on the package of the caller."""
|
||||||
|
|
||||||
def import_class():
|
# Get the caller's module and package
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
caller_frame = inspect.currentframe().f_back
|
||||||
|
module = inspect.getmodule(caller_frame)
|
||||||
|
package = module.__package__ if module else None
|
||||||
|
|
||||||
|
def import_class(*args, **kwargs):
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
# Import the module using importlib
|
# Import the module using importlib
|
||||||
module = importlib.import_module(module_name)
|
module = importlib.import_module(module_name, package=package)
|
||||||
|
|
||||||
# Get the class from the module
|
# Get the class from the module and instantiate it
|
||||||
return getattr(module, class_name)
|
cls = getattr(module, class_name)
|
||||||
|
return cls(*args, **kwargs)
|
||||||
|
|
||||||
# Return the import_class function itself, not its result
|
|
||||||
return import_class
|
return import_class
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user