Skip to content
Merged
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
10 changes: 7 additions & 3 deletions growthbook/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ContextualBanditContext,
EvaluationContext,
FeatureResult,
FeatureRule,
Experiment,
Filter,
PayloadFilters,
Expand Down Expand Up @@ -829,8 +830,11 @@ def _getHashValue(eval_context: EvaluationContext, attr: Optional[str] = None, f
(attr, val) = _getOrigHashValue(attr=attr, fallbackAttr=fallbackAttr, eval_context=eval_context)
return (attr, _hash_string(val))

def _experiment_fallback_attribute(experiment: Experiment[Any], eval_context: EvaluationContext) -> Optional[str]:
"""Experiments only use their fallback attribute when sticky bucketing is on, like JS."""
def _experiment_fallback_attribute(
experiment: Union[Experiment[Any], FeatureRule], eval_context: EvaluationContext
) -> Optional[str]:
"""Experiments and force-rule rollouts only use their fallback attribute when
sticky bucketing is on, like JS."""
if eval_context.global_ctx.options.sticky_bucket_service and not experiment.disableStickyBucketing:
return experiment.fallbackAttribute
return None
Expand Down Expand Up @@ -1348,7 +1352,7 @@ def _eval_feature(
if not _isIncludedInRollout(
seed=rule.seed or key,
hashAttribute=rule.hashAttribute,
fallbackAttribute=rule.fallbackAttribute,
fallbackAttribute=_experiment_fallback_attribute(rule, evalContext),
range=rule.range,
coverage=rule.coverage,
hashVersion=rule.hashVersion,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_force_rule_fallback_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""A force rule's rollout uses fallbackAttribute only with sticky bucketing on, like the JS SDK."""

from growthbook import GrowthBook, InMemoryStickyBucketService


def _eval(rule_extra=None, sb=None):
rule = {"force": "on", "coverage": 1.0, "hashAttribute": "id", "fallbackAttribute": "deviceId"}
rule.update(rule_extra or {})
gb = GrowthBook(
features={"flag": {"defaultValue": "off", "rules": [rule]}},
attributes={"deviceId": "dev-1"},
sticky_bucket_service=sb,
)
try:
r = gb.eval_feature("flag")
return r.value, r.source
finally:
gb.destroy()


def test_force_rule_ignores_fallback_attribute_without_sticky_bucketing():
assert _eval() == ("off", "defaultValue")


def test_force_rule_uses_fallback_attribute_with_sticky_bucketing():
assert _eval(sb=InMemoryStickyBucketService()) == ("on", "force")


def test_force_rule_ignores_fallback_attribute_when_sticky_bucketing_disabled():
rule_extra = {"disableStickyBucketing": True}
assert _eval(rule_extra, sb=InMemoryStickyBucketService()) == ("off", "defaultValue")


def test_force_rule_uses_hash_attribute_when_present():
gb = GrowthBook(
features={"flag": {"defaultValue": "off", "rules": [
{"force": "on", "coverage": 1.0, "hashAttribute": "id", "fallbackAttribute": "deviceId"}
]}},
attributes={"id": "user-1"},
)
try:
assert gb.eval_feature("flag").value == "on"
finally:
gb.destroy()
7 changes: 6 additions & 1 deletion tests/test_growthbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,16 @@ def test_unhashable_ids_use_the_fallback_attribute(user_id):
assert (res.inExperiment, res.hashAttribute, res.hashValue) == (True, "deviceId", "d1")
gb.destroy()

# Rollouts always use the rule's fallback attribute
# Rollouts use the rule's fallback attribute only with sticky bucketing on, like JS
features = {"f": {"defaultValue": "off", "rules": [
{"force": "on", "coverage": 1, "hashAttribute": "id", "fallbackAttribute": "deviceId"},
]}}
gb = GrowthBook(attributes=dict(attributes), features=features)
assert gb.eval_feature("f").source == "defaultValue"
gb.destroy()
gb = GrowthBook(
attributes=dict(attributes), features=features, sticky_bucket_service=InMemoryStickyBucketService()
)
assert gb.eval_feature("f").source == "force"
gb.destroy()
gb = GrowthBook(attributes={"id": user_id}, features=features)
Expand Down
Loading