Skip to content

Commit 0427c04

Browse files
authored
fix: Exclude identities when PERCENTAGE_SPLIT trait is undefined (#284)
1 parent 3c28ed5 commit 0427c04

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
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 = v3.3.0
4+
tag = v3.5.0

flag_engine/segments/evaluator.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,21 @@ def context_matches_condition(
264264
condition: SegmentCondition,
265265
segment_key: SupportsStr,
266266
) -> bool:
267-
context_value = (
268-
get_context_value(context, condition_property)
269-
if (condition_property := condition.get("property"))
270-
else None
271-
)
267+
context_value: ContextValue
268+
condition_property = condition["property"]
269+
condition_operator = condition["operator"]
270+
271+
if condition_operator == constants.PERCENTAGE_SPLIT and (not condition_property):
272+
# Currently, the only supported condition with a blank property
273+
# is percentage split.
274+
# In this case, we use the identity key as context value.
275+
# This is mainly to support legacy segments created before
276+
# we introduced JSONPath support.
277+
context_value = _get_identity_key(context)
278+
else:
279+
context_value = get_context_value(context, condition_property)
272280

273-
if condition["operator"] == constants.IN:
281+
if condition_operator == constants.IN:
274282
if isinstance(segment_value := condition["value"], list):
275283
in_values = segment_value
276284
else:
@@ -293,24 +301,22 @@ def context_matches_condition(
293301

294302
condition = typing.cast(StrValueSegmentCondition, condition)
295303

296-
if condition["operator"] == constants.PERCENTAGE_SPLIT:
297-
if context_value is not None:
298-
object_ids = [segment_key, context_value]
299-
elif identity_key := _get_identity_key(context):
300-
object_ids = [segment_key, identity_key]
301-
else:
304+
if condition_operator == constants.PERCENTAGE_SPLIT:
305+
if context_value is None:
302306
return False
303307

308+
object_ids = [segment_key, context_value]
309+
304310
try:
305311
float_value = float(condition["value"])
306312
except ValueError:
307313
return False
308314
return get_hashed_percentage_for_object_ids(object_ids) <= float_value
309315

310-
if condition["operator"] == constants.IS_NOT_SET:
316+
if condition_operator == constants.IS_NOT_SET:
311317
return context_value is None
312318

313-
if condition["operator"] == constants.IS_SET:
319+
if condition_operator == constants.IS_SET:
314320
return context_value is not None
315321

316322
return (
@@ -417,7 +423,7 @@ def inner(
417423

418424
def _get_identity_key(
419425
context: _EvaluationContextAnyMeta,
420-
) -> typing.Optional[SupportsStr]:
426+
) -> typing.Optional[str]:
421427
if identity_context := context.get("identity"):
422428
return identity_context.get("key")
423429
return None

0 commit comments

Comments
 (0)