Skip to main content

FallbackContext

Context workaround.

The built-in @contextmanager utility does not work well when wrapping other contexts, as the traceback is wrong when the wrapped context raises.

Attributes

AttributeTypeDescription
providedanyThe existing resource or connection to be returned directly if it is not None.
fallbackcallableA callable that returns a context manager to be used if the provided resource is None.
fb_argstuplePositional arguments to be passed to the fallback callable when initializing the context.
fb_kwargsdictKeyword arguments to be passed to the fallback callable when initializing the context.
_contextany = NoneInternal storage for the active context manager instance created by the fallback callable.

Constructor

Signature

def FallbackContext(
provided: Any,
fallback: callable,
*fb_args: tuple,
**fb_kwargs: dict
) - > null

Parameters

NameTypeDescription
providedAnyAn existing value to be returned if it is not None.
fallbackcallableA callable that returns a context manager to be used if provided is None.
*fb_argstuplePositional arguments to pass to the fallback callable.
**fb_kwargsdictKeyword arguments to pass to the fallback callable.