Coverage for src/fluree_py/http/mixin/context.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-02 03:03 +0000

1"""Mixin for managing context data in Fluree operations.""" 

2 

3from typing import Any, Generic, TypeVar, cast 

4from fluree_py.http.mixin.utils import resolve_base_class_reference 

5from fluree_py.http.protocol.mixin import HasContextData 

6 

7 

8T = TypeVar("T", bound="HasContextData") 

9"""Ensure that the type we are trying to create has a context attribute.""" 

10 

11 

12class WithContextMixin(Generic[T]): 

13 """Provides context management for Fluree operations.""" 

14 

15 def with_context(self, context: dict[str, Any]) -> T: 

16 """Updates the operation's context with new data. 

17 

18 Exceptions: 

19 TypeError: If the type parameter cannot be resolved. 

20 """ 

21 resolved_type = resolve_base_class_reference(self.__class__, "WithContextMixin") 

22 

23 # Manually create a new instance with updated context 

24 updated_fields = self.__dict__.copy() 

25 updated_fields["context"] = context 

26 return cast(T, resolved_type(**updated_fields))