Skip to content

Commit a05ab13

Browse files
committed
feat: Generic segment metadata
1 parent fa183fb commit a05ab13

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

flag_engine/context/types.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
from __future__ import annotations
66

7-
from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
7+
from typing import Any, Dict, Generic, List, Literal, Optional, Union
88

9-
from typing_extensions import NotRequired
9+
from typing_extensions import NotRequired, TypedDict
1010

11-
from flag_engine.segments.types import ConditionOperator, ContextValue, RuleType
11+
from flag_engine.segments.types import (
12+
ConditionOperator,
13+
ContextValue,
14+
MetadataT,
15+
RuleType,
16+
)
1217

1318

1419
class EnvironmentContext(TypedDict):
@@ -58,16 +63,16 @@ class FeatureContext(TypedDict):
5863
priority: NotRequired[float]
5964

6065

61-
class SegmentContext(TypedDict):
66+
class SegmentContext(TypedDict, Generic[MetadataT]):
6267
key: str
6368
name: str
6469
rules: List[SegmentRule]
6570
overrides: NotRequired[List[FeatureContext]]
66-
metadata: NotRequired[Dict[str, Any]]
71+
metadata: NotRequired[MetadataT]
6772

6873

69-
class EvaluationContext(TypedDict):
74+
class EvaluationContext(TypedDict, Generic[MetadataT]):
7075
environment: EnvironmentContext
7176
identity: NotRequired[Optional[IdentityContext]]
72-
segments: NotRequired[Dict[str, SegmentContext]]
77+
segments: NotRequired[Dict[str, SegmentContext[MetadataT]]]
7378
features: NotRequired[Dict[str, FeatureContext]]

flag_engine/result/types.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
from __future__ import annotations
66

7-
from typing import Any, Dict, List, TypedDict
7+
from typing import Any, Dict, Generic, List
88

9-
from typing_extensions import NotRequired
9+
from typing_extensions import NotRequired, TypedDict
10+
11+
from flag_engine.segments.types import MetadataT
1012

1113

1214
class FlagResult(TypedDict):
@@ -17,12 +19,12 @@ class FlagResult(TypedDict):
1719
reason: str
1820

1921

20-
class SegmentResult(TypedDict):
22+
class SegmentResult(TypedDict, Generic[MetadataT]):
2123
key: str
2224
name: str
23-
metadata: NotRequired[Dict[str, Any]]
25+
metadata: NotRequired[MetadataT]
2426

2527

26-
class EvaluationResult(TypedDict):
28+
class EvaluationResult(TypedDict, Generic[MetadataT]):
2729
flags: Dict[str, FlagResult]
28-
segments: List[SegmentResult]
30+
segments: List[SegmentResult[MetadataT]]

flag_engine/segments/types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
from typing import Any, Literal, Union, get_args
1+
from __future__ import annotations
22

3-
from typing_extensions import TypeGuard
3+
from typing import Any, Dict, Literal, Union, get_args
4+
5+
from typing_extensions import TypeGuard, TypeVar
6+
7+
MetadataT = TypeVar("MetadataT", default=Dict[str, Any])
48

59
ConditionOperator = Literal[
610
"EQUAL",

flag_engine/types/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)