Coverage for src / lexigram / contracts / data / sql / context_protocol.py: 0%
8 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
1"""Database context protocol for cross-package tenant integration."""
3from __future__ import annotations
5from typing import Any, Protocol
7if True:
8 Scope = dict[str, Any]
11class DatabaseContextProtocol(Protocol):
12 """Protocol for database context that supports tenant isolation.
14 This protocol allows packages like lexigram-tenancy to set tenant context
15 without importing from lexigram-sql directly.
17 The db_ctx is typically available in request scope at "state.db_ctx".
18 """
20 def set_tenant_from_scope(self, scope: Scope) -> Any | None:
21 """Extract and set tenant from ASGI scope.
23 Reads tenant from scope["state"]["tenant"] or scope["tenant"] and
24 sets it in the database context.
26 Args:
27 scope: ASGI connection scope.
29 Returns:
30 A token that can be used to reset the context, or None if no tenant.
31 """
32 ...
34 def reset_tenant(self, token: Any | None) -> None:
35 """Reset the tenant context using a token from set_tenant_from_scope.
37 Args:
38 token: Token returned from set_tenant_from_scope.
39 """
40 ...
43__all__ = ["DatabaseContextProtocol"]