Skip to content

Commit 121c58c

Browse files
committed
fix: added-none-check-in-context-matches-condition
1 parent f43dcc8 commit 121c58c

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

flag_engine/segments/evaluator.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import semver
88

99
from flag_engine.context.types import EvaluationContext
10-
from flag_engine.identities.traits.types import ContextValue
10+
from flag_engine.identities.traits.types import (
11+
ContextValue,
12+
map_any_value_to_trait_value,
13+
)
1114
from flag_engine.segments import constants
1215
from flag_engine.segments.models import (
1316
SegmentConditionModel,
@@ -100,7 +103,11 @@ def context_matches_condition(
100103
if condition.operator == constants.IS_SET:
101104
return context_value is not None
102105

103-
return _matches_context_value(condition, context_value) if context_value else False
106+
return (
107+
_matches_context_value(condition, context_value)
108+
if context_value is not None
109+
else False
110+
)
104111

105112

106113
def _get_trait(context: EvaluationContext, trait_key: str) -> ContextValue:

tests/unit/segments/test_segments_evaluator.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
from flag_engine.context.types import EvaluationContext
88
from flag_engine.segments import constants
9-
from flag_engine.segments.evaluator import _matches_context_value, is_context_in_segment
9+
from flag_engine.segments.evaluator import (
10+
_matches_context_value,
11+
context_matches_condition,
12+
is_context_in_segment,
13+
)
1014
from flag_engine.segments.models import (
1115
SegmentConditionModel,
1216
SegmentModel,
@@ -479,6 +483,63 @@ def test_segment_condition_matches_context_value_for_semver(
479483
assert result == expected_result
480484

481485

486+
@pytest.mark.parametrize(
487+
"context,condition,segment_key,expected_result",
488+
(
489+
(
490+
{"identity": {"traits": {trait_key_1: False}}},
491+
SegmentConditionModel(
492+
operator=constants.EQUAL,
493+
property_=trait_key_1,
494+
value="false",
495+
),
496+
"segment_key",
497+
True,
498+
),
499+
(
500+
{"identity": {"traits": {trait_key_1: True}}},
501+
SegmentConditionModel(
502+
operator=constants.EQUAL,
503+
property_=trait_key_1,
504+
value="true",
505+
),
506+
"segment_key",
507+
True,
508+
),
509+
(
510+
{"identity": {"traits": {trait_key_1: 12}}},
511+
SegmentConditionModel(
512+
operator=constants.EQUAL,
513+
property_=trait_key_1,
514+
value="12",
515+
),
516+
"segment_key",
517+
True,
518+
),
519+
(
520+
{"identity": {"traits": {trait_key_1: None}}},
521+
SegmentConditionModel(
522+
operator=constants.IS_SET,
523+
property_=trait_key_1,
524+
value="false",
525+
),
526+
"segment_key",
527+
False,
528+
),
529+
),
530+
)
531+
def test_context_matches_condition_evaluates_with_correct_casting(
532+
context: EvaluationContext,
533+
condition: SegmentConditionModel,
534+
segment_key: str,
535+
expected_result: bool,
536+
) -> None:
537+
# Given / When
538+
result = context_matches_condition(context, condition, segment_key)
539+
# Then
540+
assert result == expected_result
541+
542+
482543
@pytest.mark.parametrize(
483544
"trait_value, condition_value, expected_result",
484545
[

0 commit comments

Comments
 (0)