Skip to content

Commit 1eaca0a

Browse files
authored
Merge branch 'main' into feat/drop-pydantic-models
2 parents 2110e52 + a05befa commit 1eaca0a

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

flag_engine/segments/evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_evaluation_result(context: EvaluationContext) -> EvaluationResult:
9494

9595
def get_flag_result_from_feature_context(
9696
feature_context: FeatureContext,
97-
key: SupportsStr,
97+
key: typing.Optional[SupportsStr],
9898
) -> FlagResult:
9999
"""
100100
Get a feature value from the feature context
@@ -104,7 +104,7 @@ def get_flag_result_from_feature_context(
104104
:param key: the key to get the value for
105105
:return: the value for the key in the feature context
106106
"""
107-
if variants := feature_context.get("variants"):
107+
if key is not None and (variants := feature_context.get("variants")):
108108
percentage_value = get_hashed_percentage_for_object_ids(
109109
[feature_context["key"], key]
110110
)

tests/unit/segments/test_segments_evaluator.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def test_segment_condition_matches_context_value_for_modulo(
664664
),
665665
),
666666
)
667-
def test_get_flag_result_from_feature_context__call_returns_expected(
667+
def test_get_flag_result_from_feature_context__calls_returns_expected(
668668
percentage_value: int,
669669
expected_result: FlagResult,
670670
mocker: MockerFixture,
@@ -711,6 +711,50 @@ def test_get_flag_result_from_feature_context__call_returns_expected(
711711
)
712712

713713

714+
def test_get_flag_result_from_feature_context__null_key__calls_returns_expected(
715+
mocker: MockerFixture,
716+
) -> None:
717+
# Given
718+
expected_feature_context_key = "2"
719+
# a None key is provided (no identity context present)
720+
expected_key = None
721+
722+
get_hashed_percentage_for_object_ids_mock = mocker.patch(
723+
"flag_engine.segments.evaluator.get_hashed_percentage_for_object_ids",
724+
)
725+
726+
feature_context: FeatureContext = {
727+
"key": expected_feature_context_key,
728+
"feature_key": "1",
729+
"enabled": False,
730+
"name": "my_feature",
731+
"value": "control",
732+
"variants": [
733+
{"value": "foo", "weight": 30},
734+
{"value": "bar", "weight": 30},
735+
],
736+
}
737+
738+
# When
739+
result = get_flag_result_from_feature_context(
740+
feature_context=feature_context,
741+
key=expected_key,
742+
)
743+
744+
# Then
745+
# the value of the feature state is the default one
746+
assert result == {
747+
"enabled": False,
748+
"feature_key": "1",
749+
"name": "my_feature",
750+
"reason": "DEFAULT",
751+
"value": "control",
752+
}
753+
754+
# the function is not called as there is no key to hash against
755+
get_hashed_percentage_for_object_ids_mock.assert_not_called()
756+
757+
714758
@pytest.mark.parametrize(
715759
"property",
716760
[

0 commit comments

Comments
 (0)