Files
DB-GPT/pilot/utils/module_utils.py
2023-08-29 22:54:18 +08:00

27 lines
802 B
Python

from typing import Type
from importlib import import_module
def import_from_string(module_path: str):
try:
module_path, class_name = module_path.rsplit(".", 1)
except ValueError:
raise ImportError(f"{module_path} doesn't look like a module path")
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError:
raise ImportError(
f'Module "{module_path}" does not define a "{class_name}" attribute/class'
)
def import_from_checked_string(module_path: str, supper_cls: Type):
cls = import_from_string(module_path)
if not issubclass(cls, supper_cls):
raise ImportError(
f'Module "{module_path}" does not the subclass of {str(supper_cls)}'
)
return cls