-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all importer logic to belong in feast.importer (#2199)
* Refactor all importer logic to belong in feast.importer Signed-off-by: Felix Wang <[email protected]> * Fix unit tests error messages to match feast.errors Signed-off-by: Felix Wang <[email protected]> * Rename get_class_from_module to import_class Signed-off-by: Felix Wang <[email protected]> * Change class_type checking logic Signed-off-by: Felix Wang <[email protected]> * Change error name to FeastInvalidBaseClass Signed-off-by: Felix Wang <[email protected]>
- Loading branch information
1 parent
f969e53
commit b4d12bd
Showing
9 changed files
with
63 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,47 @@ | ||
import importlib | ||
|
||
from feast import errors | ||
from feast.errors import ( | ||
FeastClassImportError, | ||
FeastInvalidBaseClass, | ||
FeastModuleImportError, | ||
) | ||
|
||
|
||
def get_class_from_type(module_name: str, class_name: str, class_type: str): | ||
if not class_name.endswith(class_type): | ||
raise errors.FeastClassInvalidName(class_name, class_type) | ||
def import_class(module_name: str, class_name: str, class_type: str = None): | ||
""" | ||
Dynamically loads and returns a class from a module. | ||
# Try importing the module that contains the custom provider | ||
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 errors.FeastModuleImportError(module_name, class_type) from e | ||
raise FeastModuleImportError(module_name, class_name) from e | ||
|
||
# Try getting the provider class definition | ||
# 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 errors.FeastClassImportError( | ||
module_name, class_name, class_type=class_type | ||
) from None | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters