Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

@dataclass
class Fraction:
variant: str | float | int | bool | None
# whatever the variant expression evaluated to, the resolver decides what to do with it
variant: typing.Any
weight: int = 1


Expand Down Expand Up @@ -54,7 +55,7 @@ def normalize_numbers(data: typing.Any) -> typing.Any:
return data


def fractional(data: dict, *args: typing.Any) -> str | float | int | bool | None:
def fractional(data: dict, *args: typing.Any) -> typing.Any:
if not args:
logger.error("No arguments provided to fractional operator.")
return None
Expand Down
28 changes: 28 additions & 0 deletions tools/openfeature-flagd-core/tests/test_targeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,34 @@ def test_fractional_non_string_types(self) -> None:
result = fractional({}, key, ["a", 50], ["b", 50])
assert result in ("a", "b")

def test_fractional_computed_variant_expression(self) -> None:
"""A bucket variant can be any JSON Logic expression, its result is returned."""
rule = {
"fractional": [
{"var": "targetingKey"},
[{"cat": ["r", "ed"]}, 1],
[{"cat": ["bl", "ue"]}, 0],
]
}
ctx = EvaluationContext(targeting_key="user-1")
assert targeting("test-flag", rule, ctx) == "red"

def test_fractional_computed_variant_selected_by_weight(self) -> None:
rule = {
"fractional": [
{"var": "targetingKey"},
[{"cat": ["r", "ed"]}, 0],
[{"cat": ["bl", "ue"]}, 1],
]
}
ctx = EvaluationContext(targeting_key="user-1")
assert targeting("test-flag", rule, ctx) == "blue"

def test_fractional_non_scalar_variant_passed_through(self) -> None:
"""The variant isn't type checked, e.g. the result of a resolve expression."""
resolved = {"variant": "red", "reason": "SPLIT"}
assert fractional({}, "key", [resolved, 1]) == resolved

def test_fractional_top_level_array_not_explicit_key(self) -> None:
"""Top-level array is reserved for variant buckets (shorthand syntax) per ADR."""
# When no targetingKey in context, shorthand fails and returns None
Expand Down
Loading