Coverage for /home/runner/work/bijux-cli/bijux-cli/src/bijux_cli/contracts/context.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-19 23:36 +0000

1# SPDX-License-Identifier: MIT 

2# Copyright © 2025 Bijan Mousavi 

3 

4"""Defines the contract for the request-scoped context service. 

5 

6This module specifies the `ContextProtocol`, a formal interface for services 

7that manage contextual data associated with a specific operation or request. 

8This pattern allows for state to be carried implicitly through an application's 

9call stack. 

10""" 

11 

12from __future__ import annotations 

13 

14from typing import Any, Protocol, Self, runtime_checkable 

15 

16 

17@runtime_checkable 

18class ContextProtocol(Protocol): 

19 """Defines the contract for a request-scoped context object. 

20 

21 This interface specifies methods for a context that can store arbitrary 

22 key-value data and supports both synchronous (`with`) and asynchronous 

23 (`async with`) context management patterns. 

24 """ 

25 

26 def set(self, key: str, value: Any) -> None: 

27 """Sets a key-value pair in the context. 

28 

29 Args: 

30 key (str): The key to set. 

31 value (Any): The value to associate with the key. 

32 

33 Returns: 

34 None: 

35 """ 

36 ... 

37 

38 def get(self, key: str) -> Any: 

39 """Retrieves a value by key from the context. 

40 

41 Args: 

42 key (str): The key of the value to retrieve. 

43 

44 Returns: 

45 Any: The value associated with the key. 

46 """ 

47 ... 

48 

49 def clear(self) -> None: 

50 """Clears all data from the context.""" 

51 ... 

52 

53 def __enter__(self) -> Self: 

54 """Enters the synchronous context manager. 

55 

56 Returns: 

57 Self: The context instance itself. 

58 """ 

59 ... 

60 

61 def __exit__(self, _exc_type: Any, _exc_value: Any, traceback: Any) -> None: 

62 """Exits the synchronous context manager. 

63 

64 Args: 

65 _exc_type (Any): The exception type, if any. 

66 _exc_value (Any): The exception value, if any. 

67 traceback (Any): The traceback, if any. 

68 

69 Returns: 

70 None: 

71 """ 

72 ... 

73 

74 async def __aenter__(self) -> Self: 

75 """Enters the asynchronous context manager. 

76 

77 Returns: 

78 Self: The context instance itself. 

79 """ 

80 ... 

81 

82 async def __aexit__(self, _exc_type: Any, _exc_value: Any, traceback: Any) -> None: 

83 """Exits the asynchronous context manager. 

84 

85 Args: 

86 _exc_type (Any): The exception type, if any. 

87 _exc_value (Any): The exception value, if any. 

88 traceback (Any): The traceback, if any. 

89 

90 Returns: 

91 None: 

92 """ 

93 ...