Skip to content

marvin.utilities.context

Module for defining context utilities.

ScopedContext

ScopedContext provides a context management mechanism using contextvars.

This class allows setting and retrieving key-value pairs in a scoped context, which is preserved across asynchronous tasks and threads within the same context.

Attributes:

Name Type Description
_context_storage ContextVar

A context variable to store the context data.

Example

Basic Usage of ScopedContext

context = ScopedContext()
with context(key="value"):
    assert context.get("key") == "value"
# Outside the context, the value is no longer available.
assert context.get("key") is None