-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy path__init__.py
More file actions
60 lines (43 loc) · 1.64 KB
/
__init__.py
File metadata and controls
60 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import typing
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, field
from datetime import datetime
from openfeature.exception import GeneralError
__all__ = [
"EvaluationContext",
"clear_evaluation_context",
"get_evaluation_context",
"set_evaluation_context",
]
# https://openfeature.dev/specification/sections/evaluation-context#requirement-312
EvaluationContextAttribute: typing.TypeAlias = (
bool
| int
| float
| str
| datetime
| Sequence["EvaluationContextAttribute"]
| Mapping[str, "EvaluationContextAttribute"]
)
@dataclass
class EvaluationContext:
targeting_key: str | None = None
attributes: Mapping[str, EvaluationContextAttribute] = field(default_factory=dict)
def merge(self, ctx2: EvaluationContext) -> EvaluationContext:
if not (self and ctx2):
return self or ctx2
attributes = {**self.attributes, **ctx2.attributes}
targeting_key = ctx2.targeting_key or self.targeting_key
return EvaluationContext(targeting_key=targeting_key, attributes=attributes)
def get_evaluation_context() -> EvaluationContext:
return _evaluation_context
def set_evaluation_context(evaluation_context: EvaluationContext) -> None:
global _evaluation_context
if evaluation_context is None:
raise GeneralError(error_message="No api level evaluation context")
_evaluation_context = evaluation_context
def clear_evaluation_context() -> None:
set_evaluation_context(EvaluationContext())
# need to be at the bottom, because of the definition order
_evaluation_context = EvaluationContext()