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

1"""Database context protocol for cross-package tenant integration.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any, Protocol 

6 

7if True: 

8 Scope = dict[str, Any] 

9 

10 

11class DatabaseContextProtocol(Protocol): 

12 """Protocol for database context that supports tenant isolation. 

13 

14 This protocol allows packages like lexigram-tenancy to set tenant context 

15 without importing from lexigram-sql directly. 

16 

17 The db_ctx is typically available in request scope at "state.db_ctx". 

18 """ 

19 

20 def set_tenant_from_scope(self, scope: Scope) -> Any | None: 

21 """Extract and set tenant from ASGI scope. 

22 

23 Reads tenant from scope["state"]["tenant"] or scope["tenant"] and 

24 sets it in the database context. 

25 

26 Args: 

27 scope: ASGI connection scope. 

28 

29 Returns: 

30 A token that can be used to reset the context, or None if no tenant. 

31 """ 

32 ... 

33 

34 def reset_tenant(self, token: Any | None) -> None: 

35 """Reset the tenant context using a token from set_tenant_from_scope. 

36 

37 Args: 

38 token: Token returned from set_tenant_from_scope. 

39 """ 

40 ... 

41 

42 

43__all__ = ["DatabaseContextProtocol"]