Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "tests/engine_tests/engine-test-data"]
path = tests/engine_tests/engine-test-data
url = https://github.com/flagsmith/engine-test-data.git
tag = v2.2.0
branch = feat/variant-priority-sorting
1 change: 1 addition & 0 deletions flag_engine/context/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class EnvironmentContext(TypedDict):
class FeatureValue(TypedDict):
value: Any
weight: float
priority: int


class IdentityContext(TypedDict):
Expand Down
8 changes: 4 additions & 4 deletions flag_engine/segments/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ def get_flag_result_from_feature_context(
[feature_context["key"], key]
)

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

for variant in variants:
for variant in sorted(
variants,
key=operator.itemgetter("priority"),
):
limit = (weight := variant["weight"]) + start_percentage
if start_percentage <= percentage_value < limit:
return {
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/segments/test_segments_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,8 @@ def test_get_flag_result_from_feature_context__calls_returns_expected(
"name": "my_feature",
"value": "control",
"variants": [
{"value": "foo", "weight": 30},
{"value": "bar", "weight": 30},
{"value": "foo", "weight": 30, "priority": 1},
{"value": "bar", "weight": 30, "priority": 2},
],
}

Expand Down Expand Up @@ -897,8 +897,8 @@ def test_get_flag_result_from_feature_context__null_key__calls_returns_expected(
"name": "my_feature",
"value": "control",
"variants": [
{"value": "foo", "weight": 30},
{"value": "bar", "weight": 30},
{"value": "foo", "weight": 30, "priority": 1},
{"value": "bar", "weight": 30, "priority": 2},
],
}

Expand Down