Functionality for optional importing This module provides a way to import optional dependencies. In the case that the user imports some functionality from alibi-detect that is not usable due to missing optional dependencies this code is used to allow the import but replace it with an object that throws an error on use. This way we avoid errors at import time that prevent the user using functionality independent of the missing dependency.
- class alibi_detect.utils.missing_optional_dependency.MissingDependency(object_name, err, missing_dependency='all')
Missing Dependency Class.
Used to replace any object that requires unmet optional dependencies. Attribute access or calling the __call__ method on this object will raise an error.
- __call__(*args, **kwargs)
If called, raise an error.
Raise an error when attributes are accessed.
- __init__(object_name, err, missing_dependency='all')
Metaclass for MissingDependency classes.
- alibi_detect.utils.missing_optional_dependency.err_msg_template = <string.Template object>
Mapping used to ensure correct pip install message is generated if a missing optional dependency is detected. This dict is used to control two behaviours:
- When we import objects from missing dependencies we check that any ModuleNotFoundError or ImportError
corresponds to a missing optional dependency by checking the name of the missing dependency is in ERROR_TYPES. We then map this name to the corresponding optional dependency bucket that will resolve the issue.
- Some optional dependencies have multiple names such as torch and pytorch, instead of enforcing a single
naming convention across the whole code base we instead use ERROR_TYPES to capture both cases. This is done right before the pip install message is issued as this is the most robust place to capture these differences.
- alibi_detect.utils.missing_optional_dependency.import_optional(module_name, names=None)
Import a module that depends on optional dependencies Note: This function is used to import modules that depend on optional dependencies. Because it mirrors the python import functionality its return type has to be Any. Using objects imported with this function can lead to misspecification of types as Any when the developer intended to be more restrictive.
- Return type:
The module or named objects within the modules if names is not None. If the import fails due to a ModuleNotFoundError or ImportError then the requested module or named objects are replaced with instances of the MissingDependency class above.