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:
-
NoActiveScopeError–if no scope is bound.
Source code in packages/depi-core/depi/context.py
get_current_scope
¶
get_current_scope() -> Optional[ServiceScope]
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
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).