-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathimporter.py
More file actions
47 lines (39 loc) · 1.66 KB
/
importer.py
File metadata and controls
47 lines (39 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import importlib
from feast.errors import (
FeastClassImportError,
FeastInvalidBaseClass,
FeastModuleImportError,
)
def import_class(module_name: str, class_name: str, class_type: str = ""):
"""
Dynamically loads and returns a class from a module.
Args:
module_name: The name of the module.
class_name: The name of the class.
class_type: Optional name of a base class of the class.
Raises:
FeastInvalidBaseClass: If the class name does not end with the specified suffix.
FeastModuleImportError: If the module cannot be imported.
FeastClassImportError: If the class cannot be imported.
"""
# Try importing the module.
try:
module = importlib.import_module(module_name)
except Exception as e:
# The original exception can be anything - either module not found,
# or any other kind of error happening during the module import time.
# So we should include the original error as well in the stack trace.
raise FeastModuleImportError(module_name, class_name) from e
# Try getting the class.
try:
_class = getattr(module, class_name)
except AttributeError:
# This can only be one type of error, when class_name attribute does not exist in the module
# So we don't have to include the original exception here
raise FeastClassImportError(module_name, class_name) from None
# Check if the class is a subclass of the base class.
if class_type and not any(
base_class.__name__ == class_type for base_class in _class.mro()
):
raise FeastInvalidBaseClass(class_name, class_type)
return _class