Function to generate an “unavailable” base class
This function returns a custom class that wraps the
ModuleUnavailable instance returned by
attempt_import() when the target module is not available.
Any attempt to instantiate this class (or a class derived from it)
or access a class attribute will raise the
DeferredImportError from the wrapped
ModuleUnavailable object.
- Parameters:
unavailable_module (ModuleUnavailable) – The ModuleUnavailable instance (from
attempt_import()) to use to generate the
DeferredImportError.
Example
Declaring a class that inherits from an optional dependency:
>>> from pyomo.common.dependencies import attempt_import, UnavailableClass
>>> bogus, bogus_available = attempt_import('bogus_unavailable_class')
>>> class MyPlugin(bogus.plugin if bogus_available else UnavailableClass(bogus)):
... pass
Attempting to instantiate the derived class generates an exception
when the module is unavailable:
>>> MyPlugin()
Traceback (most recent call last):
...
pyomo.common.dependencies.DeferredImportError: The class 'MyPlugin' cannot be
created because a needed optional dependency was not found (import raised
ModuleNotFoundError: No module named 'bogus_unavailable_class')
As does attempting to access class attributes on the derived class:
>>> MyPlugin.create_instance()
Traceback (most recent call last):
...
pyomo.common.dependencies.DeferredImportError: The class attribute
'MyPlugin.create_instance' is not available because a needed optional
dependency was not found (import raised ModuleNotFoundError: No module
named 'bogus_unavailable_class')