Skip to content

Commit 93418fd

Browse files
committed
TraitValue -> ContextValue
1 parent 42829fa commit 93418fd

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

flag_engine/context/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from typing_extensions import NotRequired
1010

11-
from flag_engine.identities.traits.types import TraitValue
11+
from flag_engine.identities.traits.types import ContextValue
1212
from flag_engine.utils.types import SupportsStr
1313

1414

@@ -20,7 +20,7 @@ class EnvironmentContext(TypedDict):
2020
class IdentityContext(TypedDict):
2121
identifier: str
2222
key: SupportsStr
23-
traits: NotRequired[Dict[str, TraitValue]]
23+
traits: NotRequired[Dict[str, ContextValue]]
2424

2525

2626
class EvaluationContext(TypedDict):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from pydantic import BaseModel, Field
22

3-
from flag_engine.identities.traits.types import TraitValue
3+
from flag_engine.identities.traits.types import ContextValue
44

55

66
class TraitModel(BaseModel):
77
trait_key: str
8-
trait_value: TraitValue = Field(...)
8+
trait_value: ContextValue = Field(...)

flag_engine/identities/traits/types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
from flag_engine.identities.traits.constants import TRAIT_STRING_VALUE_MAX_LENGTH
1010

11-
_UnconstrainedTraitValue = Union[None, int, float, bool, str]
11+
_UnconstrainedContextValue = Union[None, int, float, bool, str]
1212

1313

14-
def map_any_value_to_trait_value(value: Any) -> _UnconstrainedTraitValue:
14+
def map_any_value_to_trait_value(value: Any) -> _UnconstrainedContextValue:
1515
"""
1616
Try to coerce a value of arbitrary type to a trait value type.
1717
Union member-specific constraints, such as max string value length, are ignored here.
@@ -36,19 +36,19 @@ def map_any_value_to_trait_value(value: Any) -> _UnconstrainedTraitValue:
3636
_float_pattern = re.compile(r"-?[0-9]+\.[0-9]+")
3737

3838

39-
def _map_string_value_to_trait_value(value: str) -> _UnconstrainedTraitValue:
39+
def _map_string_value_to_trait_value(value: str) -> _UnconstrainedContextValue:
4040
if _int_pattern.fullmatch(value):
4141
return int(value)
4242
if _float_pattern.fullmatch(value):
4343
return float(value)
4444
return value
4545

4646

47-
def _is_trait_value(value: Any) -> TypeGuard[_UnconstrainedTraitValue]:
48-
return isinstance(value, get_args(_UnconstrainedTraitValue))
47+
def _is_trait_value(value: Any) -> TypeGuard[_UnconstrainedContextValue]:
48+
return isinstance(value, get_args(_UnconstrainedContextValue))
4949

5050

51-
TraitValue = Annotated[
51+
ContextValue = Annotated[
5252
Union[
5353
None,
5454
StrictBool,

flag_engine/segments/evaluator.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import semver
99

1010
from flag_engine.context.types import EvaluationContext
11-
from flag_engine.identities.traits.types import TraitValue
11+
from flag_engine.identities.traits.types import ContextValue
1212
from flag_engine.segments import constants
1313
from flag_engine.segments.models import (
1414
SegmentConditionModel,
@@ -104,7 +104,7 @@ def context_matches_condition(
104104
return _matches_context_value(condition, context_value) if context_value else False
105105

106106

107-
def _get_trait(context: EvaluationContext, trait_key: str) -> TraitValue:
107+
def _get_trait(context: EvaluationContext, trait_key: str) -> ContextValue:
108108
return (
109109
identity_context["traits"][trait_key]
110110
if (identity_context := context["identity"])
@@ -115,7 +115,7 @@ def _get_trait(context: EvaluationContext, trait_key: str) -> TraitValue:
115115
def get_context_value(
116116
context: EvaluationContext,
117117
property: str,
118-
) -> TraitValue:
118+
) -> ContextValue:
119119
getter = CONTEXT_VALUE_GETTERS_BY_PROPERTY.get(property) or partial(
120120
_get_trait,
121121
trait_key=property,
@@ -128,7 +128,7 @@ def get_context_value(
128128

129129
def _matches_context_value(
130130
condition: SegmentConditionModel,
131-
context_value: TraitValue,
131+
context_value: ContextValue,
132132
) -> bool:
133133
if matcher := MATCHERS_BY_OPERATOR.get(condition.operator):
134134
return matcher(condition.value, context_value)
@@ -138,14 +138,14 @@ def _matches_context_value(
138138

139139
def _evaluate_not_contains(
140140
segment_value: typing.Optional[str],
141-
context_value: TraitValue,
141+
context_value: ContextValue,
142142
) -> bool:
143143
return isinstance(context_value, str) and str(segment_value) not in context_value
144144

145145

146146
def _evaluate_regex(
147147
segment_value: typing.Optional[str],
148-
context_value: TraitValue,
148+
context_value: ContextValue,
149149
) -> bool:
150150
return (
151151
context_value is not None
@@ -155,7 +155,7 @@ def _evaluate_regex(
155155

156156
def _evaluate_modulo(
157157
segment_value: typing.Optional[str],
158-
context_value: TraitValue,
158+
context_value: ContextValue,
159159
) -> bool:
160160
if not isinstance(context_value, (int, float)):
161161
return False
@@ -174,7 +174,7 @@ def _evaluate_modulo(
174174

175175

176176
def _evaluate_in(
177-
segment_value: typing.Optional[str], context_value: TraitValue
177+
segment_value: typing.Optional[str], context_value: ContextValue
178178
) -> bool:
179179
if segment_value:
180180
if isinstance(context_value, str):
@@ -188,11 +188,11 @@ def _evaluate_in(
188188

189189
def _context_value_typed(
190190
func: typing.Callable[..., bool],
191-
) -> typing.Callable[[typing.Optional[str], TraitValue], bool]:
191+
) -> typing.Callable[[typing.Optional[str], ContextValue], bool]:
192192
@wraps(func)
193193
def inner(
194194
segment_value: typing.Optional[str],
195-
context_value: typing.Union[TraitValue, semver.Version],
195+
context_value: typing.Union[ContextValue, semver.Version],
196196
) -> bool:
197197
with suppress(TypeError, ValueError):
198198
if isinstance(context_value, str) and is_semver(segment_value):
@@ -207,7 +207,7 @@ def inner(
207207

208208

209209
MATCHERS_BY_OPERATOR: typing.Dict[
210-
ConditionOperator, typing.Callable[[typing.Optional[str], TraitValue], bool]
210+
ConditionOperator, typing.Callable[[typing.Optional[str], ContextValue], bool]
211211
] = {
212212
constants.NOT_CONTAINS: _evaluate_not_contains,
213213
constants.REGEX: _evaluate_regex,

flag_engine/utils/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import semver
55

6-
from flag_engine.identities.traits.types import TraitValue
6+
from flag_engine.identities.traits.types import ContextValue
77
from flag_engine.utils.semver import remove_semver_suffix
88

99

@@ -15,7 +15,7 @@ def __str__(self) -> str: # pragma: no cover
1515
@singledispatch
1616
def get_casting_function(
1717
input_: object,
18-
) -> typing.Callable[..., TraitValue]:
18+
) -> typing.Callable[..., ContextValue]:
1919
"""
2020
This function returns a callable to cast a value to the same type as input_
2121
>>> assert get_casting_function("a string") == str

0 commit comments

Comments
 (0)