alibi_detect.utils.missing_optional_dependency module

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')[source]

Bases: object

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)[source]

If called, raise an error.

__getattr__(key)[source]

Raise an error when attributes are accessed.

__init__(object_name, err, missing_dependency='all')[source]

Metaclass for MissingDependency classes.

Parameters:
  • object_name (str) – Name of object we are replacing

  • err (Union[ModuleNotFoundError, ImportError]) – Error to be raised when the class is initialized or used

  • missing_dependency (str) – Name of missing dependency required for object

property err_msg: str

Generate error message informing user to install missing dependencies.

Return type:

str

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:

  1. 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.

  2. 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)[source]

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.

Parameters:
  • module_name (str) – The module to import

  • names (Optional[List[str]]) – The names to import from the module. If None, all names are imported.

Return type:

Any

Returns:

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.