diff --git a/flag_engine/context/types.py b/flag_engine/context/types.py index 65ddd508..3cd4d9c2 100644 --- a/flag_engine/context/types.py +++ b/flag_engine/context/types.py @@ -1,10 +1,10 @@ # generated by datamodel-codegen: -# filename: https://raw.githubusercontent.com/Flagsmith/flagsmith/chore/features-contexts-in-eval-context-schema/sdk/evaluation-context.json # noqa: E501 -# timestamp: 2025-08-11T18:17:29+00:00 +# filename: https://raw.githubusercontent.com/Flagsmith/flagsmith/main/sdk/evaluation-context.json # noqa: E501 +# timestamp: 2025-08-25T11:10:31+00:00 from __future__ import annotations -from typing import Any, Dict, List, Optional, TypedDict, Union +from typing import Any, Dict, List, Literal, Optional, TypedDict, Union from typing_extensions import NotRequired @@ -27,12 +27,21 @@ class IdentityContext(TypedDict): traits: NotRequired[Dict[str, Optional[Union[str, float, bool]]]] -class SegmentCondition(TypedDict): - property: NotRequired[str] +class StrValueSegmentCondition(TypedDict): + property: str operator: ConditionOperator value: str +class InOperatorSegmentCondition(TypedDict): + property: str + operator: Literal["IN"] + value: List[str] + + +SegmentCondition = Union[StrValueSegmentCondition, InOperatorSegmentCondition] + + class SegmentRule(TypedDict): type: RuleType conditions: NotRequired[List[SegmentCondition]] diff --git a/flag_engine/segments/evaluator.py b/flag_engine/segments/evaluator.py index 2ecbbe0f..5e47d270 100644 --- a/flag_engine/segments/evaluator.py +++ b/flag_engine/segments/evaluator.py @@ -16,6 +16,7 @@ SegmentCondition, SegmentContext, SegmentRule, + StrValueSegmentCondition, ) from flag_engine.environments.models import EnvironmentModel from flag_engine.identities.models import IdentityModel @@ -235,6 +236,29 @@ def context_matches_condition( else None ) + if condition["operator"] == constants.IN: + if isinstance(segment_value := condition["value"], list): + in_values = segment_value + else: + try: + in_values = json.loads(segment_value) + # Only accept JSON lists. + # Ideally, we should use something like pydantic.TypeAdapter[list[str]], + # but we aim to ditch the pydantic dependency in the future. + if not isinstance(in_values, list): + raise ValueError + except ValueError: + in_values = segment_value.split(",") + in_values = [str(value) for value in in_values] + # Guard against comparing boolean values to numeric strings. + if isinstance(context_value, int) and not ( + context_value is True or context_value is False + ): + context_value = str(context_value) + return context_value in in_values + + condition = typing.cast(StrValueSegmentCondition, condition) + if condition["operator"] == constants.PERCENTAGE_SPLIT: if context_value is not None: object_ids = [segment_key, context_value] @@ -270,7 +294,7 @@ def get_context_value( def _matches_context_value( - condition: SegmentCondition, + condition: StrValueSegmentCondition, context_value: ContextValue, ) -> bool: if matcher := MATCHERS_BY_OPERATOR.get(condition["operator"]): @@ -316,29 +340,6 @@ def _evaluate_modulo( return context_value % divisor == remainder -def _evaluate_in( - segment_value: typing.Optional[str], context_value: ContextValue -) -> bool: - if segment_value: - try: - in_values = json.loads(segment_value) - # Only accept JSON lists. - # Ideally, we should use something like pydantic.TypeAdapter[list[str]], - # but we aim to ditch the pydantic dependency in the future. - if not isinstance(in_values, list): - raise ValueError - in_values = [str(value) for value in in_values] - except ValueError: - in_values = segment_value.split(",") - # Guard against comparing boolean values to numeric strings. - if isinstance(context_value, int) and not any( - context_value is x for x in (False, True) - ): - context_value = str(context_value) - return context_value in in_values - return False - - def _context_value_typed( func: typing.Callable[..., bool], ) -> typing.Callable[[typing.Optional[str], ContextValue], bool]: @@ -365,7 +366,6 @@ def inner( constants.NOT_CONTAINS: _evaluate_not_contains, constants.REGEX: _evaluate_regex, constants.MODULO: _evaluate_modulo, - constants.IN: _evaluate_in, constants.EQUAL: _context_value_typed(operator.eq), constants.GREATER_THAN: _context_value_typed(operator.gt), constants.GREATER_THAN_INCLUSIVE: _context_value_typed(operator.ge), diff --git a/tests/unit/segments/fixtures.py b/tests/unit/segments/fixtures.py index 96e90726..934311c4 100644 --- a/tests/unit/segments/fixtures.py +++ b/tests/unit/segments/fixtures.py @@ -1,4 +1,4 @@ -from flag_engine.context.types import SegmentCondition, SegmentContext, SegmentRule +from flag_engine.context.types import SegmentContext from flag_engine.segments import constants trait_key_1 = "email" @@ -10,141 +10,141 @@ trait_key_3 = "date_joined" trait_value_3 = "2021-01-01" -empty_segment = SegmentContext(key=str(1), name="empty_segment", rules=[]) +empty_segment = {"key": "1", "name": "empty_segment", "rules": []} -segment_single_condition = SegmentContext( - key=str(2), - name="segment_one_condition", - rules=[ - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value=trait_value_1, - ) +segment_single_condition: SegmentContext = { + "key": "2", + "name": "segment_one_condition", + "rules": [ + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": trait_value_1, + } ], - ) + } ], -) +} -segment_multiple_conditions_all = SegmentContext( - key=str(3), - name="segment_multiple_conditions_all", - rules=[ - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value=trait_value_1, - ), - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_2, - value=trait_value_2, - ), +segment_multiple_conditions_all: SegmentContext = { + "key": "3", + "name": "segment_multiple_conditions_all", + "rules": [ + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": trait_value_1, + }, + { + "operator": constants.EQUAL, + "property": trait_key_2, + "value": trait_value_2, + }, ], - ) + } ], -) +} -segment_multiple_conditions_any = SegmentContext( - key=str(4), - name="segment_multiple_conditions_all", - rules=[ - SegmentRule( - type=constants.ANY_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value=trait_value_1, - ), - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_2, - value=trait_value_2, - ), +segment_multiple_conditions_any: SegmentContext = { + "key": "4", + "name": "segment_multiple_conditions_all", + "rules": [ + { + "type": constants.ANY_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": trait_value_1, + }, + { + "operator": constants.EQUAL, + "property": trait_key_2, + "value": trait_value_2, + }, ], - ) + } ], -) +} -segment_nested_rules = SegmentContext( - key=str(5), - name="segment_nested_rules_all", - rules=[ - SegmentRule( - type=constants.ALL_RULE, - rules=[ - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value=trait_value_1, - ), - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_2, - value=trait_value_2, - ), +segment_nested_rules: SegmentContext = { + "key": "5", + "name": "segment_nested_rules_all", + "rules": [ + { + "type": constants.ALL_RULE, + "rules": [ + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": trait_value_1, + }, + { + "operator": constants.EQUAL, + "property": trait_key_2, + "value": trait_value_2, + }, ], - ), - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_3, - value=trait_value_3, - ) + }, + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_3, + "value": trait_value_3, + } ], - ), + }, ], - ) + } ], -) +} -segment_conditions_and_nested_rules = SegmentContext( - key=str(6), - name="segment_multiple_conditions_all_and_nested_rules", - rules=[ - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value=trait_value_1, - ) +segment_conditions_and_nested_rules: SegmentContext = { + "key": "6", + "name": "segment_multiple_conditions_all_and_nested_rules", + "rules": [ + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": trait_value_1, + } ], - rules=[ - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_2, - value=trait_value_2, - ), + "rules": [ + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_2, + "value": trait_value_2, + }, ], - ), - SegmentRule( - type=constants.ALL_RULE, - conditions=[ - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_3, - value=trait_value_3, - ) + }, + { + "type": constants.ALL_RULE, + "conditions": [ + { + "operator": constants.EQUAL, + "property": trait_key_3, + "value": trait_value_3, + } ], - ), + }, ], - ) + } ], -) +} diff --git a/tests/unit/segments/test_segments_evaluator.py b/tests/unit/segments/test_segments_evaluator.py index 648a19ed..6313f733 100644 --- a/tests/unit/segments/test_segments_evaluator.py +++ b/tests/unit/segments/test_segments_evaluator.py @@ -11,6 +11,7 @@ FeatureContext, SegmentCondition, SegmentContext, + StrValueSegmentCondition, ) from flag_engine.environments.models import EnvironmentModel from flag_engine.features.models import FeatureModel, FeatureStateModel @@ -242,10 +243,10 @@ def test_context_in_segment_percentage_split( expected_result: bool, ) -> None: # Given - segment_context = SegmentContext( - key="1", - name="% split", - rules=[ + segment_context: SegmentContext = { + "key": "1", + "name": "% split", + "rules": [ { "type": constants.ALL_RULE, "conditions": [], @@ -264,7 +265,7 @@ def test_context_in_segment_percentage_split( ], } ], - ) + } mock_get_hashed_percentage = mocker.patch( "flag_engine.segments.evaluator.get_hashed_percentage_for_object_ids" @@ -326,10 +327,10 @@ def test_context_in_segment_percentage_split__trait_value__calls_expected( assert context["identity"] is not None context["identity"]["traits"]["custom_trait"] = "custom_value" - segment_context = SegmentContext( - key="1", - name="% split", - rules=[ + segment_context: SegmentContext = { + "key": "1", + "name": "% split", + "rules": [ { "type": constants.ALL_RULE, "conditions": [], @@ -348,7 +349,7 @@ def test_context_in_segment_percentage_split__trait_value__calls_expected( ], } ], - ) + } mock_get_hashed_percentage = mocker.patch( "flag_engine.segments.evaluator.get_hashed_percentage_for_object_ids" @@ -472,35 +473,6 @@ def test_context_in_segment_is_set_and_is_not_set( (constants.REGEX, None, r"[a-z]", False), (constants.REGEX, "foo", 12, False), (constants.REGEX, 1, "1", True), - (constants.IN, "foo", "", False), - (constants.IN, "foo", "foo,bar", True), - (constants.IN, "bar", "foo,bar", True), - (constants.IN, "foo", "foo", True), - (constants.IN, 1, "1,2,3,4", True), - (constants.IN, 1, "", False), - (constants.IN, 1, "1", True), - (constants.IN, 1, None, False), - (constants.IN, 1, None, False), - (constants.IN, "foo", "", False), - (constants.IN, "foo", "foo,bar", True), - (constants.IN, "bar", "foo,bar", True), - (constants.IN, "foo", "foo", True), - (constants.IN, 1, "1,2,3,4", True), - (constants.IN, 1, "", False), - (constants.IN, 1, "1", True), - (constants.IN, 1, None, False), - (constants.IN, 1, None, False), - (constants.IN, "foo", "[]", False), - (constants.IN, "foo", '["foo","bar"]', True), - (constants.IN, "bar", '["foo","bar"]', True), - (constants.IN, "foo", '["foo"]', True), - (constants.IN, 1, "[1,2,3,4]", True), - (constants.IN, 1, '["1","2","3","4"]', True), - (constants.IN, 1, "[]", False), - (constants.IN, 1, "[1]", True), - (constants.IN, 1, '["1"]', True), - (constants.IN, 1, None, False), - (constants.IN, 1, None, False), ), ) def test_segment_condition_matches_context_value( @@ -510,7 +482,7 @@ def test_segment_condition_matches_context_value( expected_result: bool, ) -> None: # Given - segment_condition: SegmentCondition = { + segment_condition: StrValueSegmentCondition = { "operator": operator, "property": "foo", "value": condition_value, @@ -528,11 +500,11 @@ def test_segment_condition__unsupported_operator__return_false( ) -> None: # Given mocker.patch("flag_engine.segments.evaluator.MATCHERS_BY_OPERATOR", new={}) - segment_condition = SegmentCondition( - operator=constants.EQUAL, - property="x", - value="foo", - ) + segment_condition: StrValueSegmentCondition = { + "operator": constants.EQUAL, + "property": "x", + "value": "foo", + } trait_value = "foo" # When @@ -574,11 +546,11 @@ def test_segment_condition_matches_context_value_for_semver( expected_result: bool, ) -> None: # Given - segment_condition = SegmentCondition( - operator=operator, - property="version", - value=condition_value, - ) + segment_condition: StrValueSegmentCondition = { + "operator": operator, + "property": "version", + "value": condition_value, + } # When result = _matches_context_value(segment_condition, trait_value) @@ -589,48 +561,194 @@ def test_segment_condition_matches_context_value_for_semver( @pytest.mark.parametrize( "context,condition,segment_key,expected_result", - ( + [ ( {"identity": {"traits": {trait_key_1: False}}}, - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value="false", - ), + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": "false", + }, "segment_key", True, ), ( {"identity": {"traits": {trait_key_1: True}}}, - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value="true", - ), + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": "true", + }, "segment_key", True, ), ( {"identity": {"traits": {trait_key_1: 12}}}, - SegmentCondition( - operator=constants.EQUAL, - property=trait_key_1, - value="12", - ), + { + "operator": constants.EQUAL, + "property": trait_key_1, + "value": "12", + }, "segment_key", True, ), ( {"identity": {"traits": {trait_key_1: None}}}, - SegmentCondition( - operator=constants.IS_SET, - property=trait_key_1, - value="false", - ), + { + "operator": constants.IS_SET, + "property": trait_key_1, + "value": "false", + }, "segment_key", False, ), - ), + ( + {"identity": {"traits": {trait_key_1: "foo"}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "foo,bar", + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: "bar"}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "foo,bar", + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: "baz"}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "foo,bar", + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: 1}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "1,2,3,4", + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: 5}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "1,2,3,4", + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: "foo"}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": '["foo","bar"]', + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: "baz"}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": '["foo","bar"]', + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: 1}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "[1,2,3,4]", + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: 5}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "[1,2,3,4]", + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: 1}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": '["1","2","3","4"]', + }, + "segment_key", + True, + ), + ( + {"identity": {"traits": {trait_key_1: 5}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": '["1","2","3","4"]', + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: None}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": "foo,bar", + }, + "segment_key", + False, + ), + ( + {"identity": {"traits": {trait_key_1: 1}}}, + { + "operator": constants.IN, + "property": trait_key_1, + "value": [1, 2, 3, 4], + }, + "segment_key", + True, + ), + ( + { + "identity": { + "traits": { + trait_key_1: '"I am a valid JSON literal but not a list"' + } + } + }, + { + "operator": constants.IN, + "property": trait_key_1, + "value": '"I am a valid JSON literal but not a list"', + }, + "segment_key", + True, + ), + ], ) def test_context_matches_condition( context: EvaluationContext, @@ -665,11 +783,11 @@ def test_segment_condition_matches_context_value_for_modulo( expected_result: bool, ) -> None: # Given - segment_condition = SegmentCondition( - operator=constants.MODULO, - property="version", - value=condition_value, - ) + segment_condition: StrValueSegmentCondition = { + "operator": constants.MODULO, + "property": "version", + "value": condition_value, + } # When result = _matches_context_value(segment_condition, trait_value)