Skip to content

Ambient scope

The request scope a framework adapter opens is bound to a ContextVar in depi.context. Code running inside a request can reach it without it being passed down the call stack. All five functions are re-exported from depi.

See Lifetimes and scopes.

Ambient scope tracking.

Framework integrations bind a :class:~depi.services.ServiceScope to the current request or task context so view functions can reach it without it being threaded through every call.

This lives in core rather than in an integration on purpose: every integration needs the same contextvar. If each one owned a private one, a scope opened by the Flask integration would be invisible to anything reading through another, and nested/mixed stacks (an ASGI app mounting a WSGI app, say) would silently resolve against the wrong scope.

current_scope

current_scope() -> ServiceScope

Return the scope bound to the current context.

Raises:

Source code in packages/depi-core/depi/context.py
def current_scope() -> 'ServiceScope':
    """
    Return the scope bound to the current context.

    Raises:
        NoActiveScopeError: if no scope is bound.
    """
    scope = _current_scope.get()
    if scope is None:
        raise NoActiveScopeError(
            "No active depi scope. Ensure the integration's setup(app) ran and that "
            "this code runs inside a request handled by its middleware."
        )
    return scope

get_current_scope

get_current_scope() -> Optional[ServiceScope]

Return the scope bound to the current context, or None if there is none.

Source code in packages/depi-core/depi/context.py
def get_current_scope() -> Optional['ServiceScope']:
    """Return the scope bound to the current context, or None if there is none."""
    return _current_scope.get()

set_current_scope

set_current_scope(scope: Optional[ServiceScope]) -> Token

Bind a scope to the current context.

Returns the token needed to restore the previous value; pass it to :func:reset_current_scope. Prefer :func:use_scope where the bind and restore happen in the same frame.

Source code in packages/depi-core/depi/context.py
def set_current_scope(scope: Optional['ServiceScope']) -> Token:
    """
    Bind a scope to the current context.

    Returns the token needed to restore the previous value; pass it to
    :func:`reset_current_scope`. Prefer :func:`use_scope` where the bind and
    restore happen in the same frame.
    """
    return _current_scope.set(scope)

reset_current_scope

reset_current_scope(token: Token) -> None

Restore the scope that was bound before token was issued.

Source code in packages/depi-core/depi/context.py
def reset_current_scope(token: Token) -> None:
    """Restore the scope that was bound before ``token`` was issued."""
    _current_scope.reset(token)

use_scope

use_scope(scope: ServiceScope) -> Iterator[ServiceScope]

Bind scope for the duration of the block, then restore the previous one.

Note this only binds the scope; it does not dispose it. Disposal stays with whoever created the scope, since integrations differ on when it is safe (Flask at teardown_request, ASGI after the response body is sent).

Source code in packages/depi-core/depi/context.py
@contextmanager
def use_scope(scope: 'ServiceScope') -> Iterator['ServiceScope']:
    """
    Bind ``scope`` for the duration of the block, then restore the previous one.

    Note this only binds the scope; it does not dispose it. Disposal stays with
    whoever created the scope, since integrations differ on when it is safe
    (Flask at teardown_request, ASGI after the response body is sent).
    """
    token = _current_scope.set(scope)
    try:
        yield scope
    finally:
        _current_scope.reset(token)