Skip to content

Commit 8e790b2

Browse files
authored
feat!: Use FeatureValue.priority for multivariate evaluations (#268)
1 parent 789d737 commit 8e790b2

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "tests/engine_tests/engine-test-data"]
22
path = tests/engine_tests/engine-test-data
33
url = https://github.com/flagsmith/engine-test-data.git
4-
tag = v2.2.0
4+
tag = v2.4.0

flag_engine/context/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class EnvironmentContext(TypedDict):
2424
class FeatureValue(TypedDict):
2525
value: Any
2626
weight: float
27+
priority: int
2728

2829

2930
class IdentityContext(TypedDict):

flag_engine/segments/evaluator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ def get_flag_result_from_feature_context(
131131
[feature_context["key"], key]
132132
)
133133

134-
# We expect `variants` to be pre-sorted in order of persistence. This gives us a
135-
# way to ensure that the same value is returned every time we use the same
136-
# percentage value.
137134
start_percentage = 0.0
138135

139-
for variant in variants:
136+
for variant in sorted(
137+
variants,
138+
key=operator.itemgetter("priority"),
139+
):
140140
limit = (weight := variant["weight"]) + start_percentage
141141
if start_percentage <= percentage_value < limit:
142142
return {
Submodule engine-test-data updated 35 files

tests/unit/segments/test_segments_evaluator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,8 @@ def test_get_flag_result_from_feature_context__calls_returns_expected(
855855
"name": "my_feature",
856856
"value": "control",
857857
"variants": [
858-
{"value": "foo", "weight": 30},
859-
{"value": "bar", "weight": 30},
858+
{"value": "foo", "weight": 30, "priority": 1},
859+
{"value": "bar", "weight": 30, "priority": 2},
860860
],
861861
}
862862

@@ -897,8 +897,8 @@ def test_get_flag_result_from_feature_context__null_key__calls_returns_expected(
897897
"name": "my_feature",
898898
"value": "control",
899899
"variants": [
900-
{"value": "foo", "weight": 30},
901-
{"value": "bar", "weight": 30},
900+
{"value": "foo", "weight": 30, "priority": 1},
901+
{"value": "bar", "weight": 30, "priority": 2},
902902
],
903903
}
904904

0 commit comments

Comments
 (0)