UnavailableClass
(function from pyomo.common.dependencies)
- pyomo.common.dependencies.UnavailableClass(unavailable_module)[source]
Function to generate an “unavailable” base class
This function returns a custom class that wraps the
ModuleUnavailableinstance returned byattempt_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 theDeferredImportErrorfrom the wrappedModuleUnavailableobject.- Parameters:
unavailable_module (ModuleUnavailable) – The
ModuleUnavailableinstance (fromattempt_import()) to use to generate theDeferredImportError.
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')