fix(proxy): import installed callback modules before file fallback

This commit is contained in:
SamiKolarkar 2026-06-26 15:13:44 +05:30
parent 52e5b3ae98
commit b72ebb511e

View file

@ -35,29 +35,36 @@ def get_instance_fn(value: str, config_file_path: Optional[str] = None) -> Any:
instance_name = parts[-1]
# If config_file_path is provided, use it to determine the module spec and load the module
if config_file_path is not None:
directory = os.path.dirname(config_file_path)
module_file_path = os.path.join(directory, *module_name.split("."))
module_file_path += ".py"
try:
# First try a normal Python import (site-packages, installed modules, etc.)
module = importlib.import_module(module_name)
# Check if the file exists before trying to load it
if not os.path.exists(module_file_path):
raise ImportError(f"Could not find module file {module_file_path}")
except ImportError:
# Fall back to loading a local callback relative to the config file
if config_file_path is None:
raise
spec = importlib.util.spec_from_file_location(module_name, module_file_path) # type: ignore
if spec is None:
raise ImportError(
f"Could not find a module specification for {module_file_path}"
)
module = importlib.util.module_from_spec(spec) # type: ignore
if spec.loader is None:
raise ImportError(
f"Could not find a module loader for {module_file_path}"
)
spec.loader.exec_module(module) # type: ignore
else:
# Dynamically import the module
module = importlib.import_module(module_name)
directory = os.path.dirname(config_file_path)
module_file_path = os.path.join(directory, *module_name.split("."))
module_file_path += ".py"
if not os.path.exists(module_file_path):
raise ImportError(f"Could not find module file {module_file_path}")
spec = importlib.util.spec_from_file_location(module_name, module_file_path)
if spec is None:
raise ImportError(
f"Could not find a module specification for {module_file_path}"
)
module = importlib.util.module_from_spec(spec)
if spec.loader is None:
raise ImportError(
f"Could not find a module loader for {module_file_path}"
)
spec.loader.exec_module(module)
# Get the instance from the module
instance = getattr(module, instance_name)