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
20 changes: 17 additions & 3 deletions growthbook/common_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ def to_dict(self) -> Dict[str, Any]:
"rules": [rule.to_dict() for rule in self.rules],
}


# Default for FeatureRule's `force`, so that a rule forcing null can be told
# apart from a rule with no `force` at all.
_NO_FORCE: Any = object()


class FeatureRule(object):
def __init__(
self,
Expand All @@ -369,7 +375,7 @@ def __init__(
coverage: Optional[float] = None,
condition: Optional[Dict[str, Any]] = None,
namespace: Optional[Tuple[str, float, float]] = None,
force: Optional[Any] = None,
force: Optional[Any] = _NO_FORCE,
hashAttribute: str = "id",
fallbackAttribute: Optional[str] = None,
hashVersion: Optional[int] = None,
Expand Down Expand Up @@ -401,7 +407,10 @@ def __init__(
self.coverage = coverage
self.condition = condition
self.namespace = namespace
self.force = force
# A rule given `force` is a force rule even when the value is null, as
# in the JS SDK (`"force" in rule`): null is a valid JSON feature value.
self._forces_null = force is None
self.force = None if force is _NO_FORCE else force
self.hashAttribute = hashAttribute
self.fallbackAttribute = fallbackAttribute
self.hashVersion = hashVersion or 1
Expand All @@ -425,6 +434,11 @@ def __init__(
self.contextualBanditRef = contextualBanditRef
self.contextualVariations = contextualVariations

@property
def is_force_rule(self) -> bool:
"""Whether the rule serves `force` (which may be null) instead of running an experiment."""
return self.force is not None or self._forces_null

def to_dict(self) -> Dict[str, Any]:
data: Dict[str, Any] = {}
if self.id:
Expand All @@ -442,7 +456,7 @@ def to_dict(self) -> Dict[str, Any]:
data["condition"] = self.condition
if self.namespace is not None:
data["namespace"] = self.namespace
if self.force is not None:
if self.is_force_rule:
data["force"] = self.force
if self.hashAttribute != "id":
data["hashAttribute"] = self.hashAttribute
Expand Down
19 changes: 12 additions & 7 deletions growthbook/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,19 +1347,24 @@ def _eval_feature(
logger.debug("Skip rule because of failing prerequisite, feature %s", key)
continue

if rule.condition:
if not evalCondition(evalContext.user.attributes, rule.condition, evalContext.global_ctx.saved_groups):
logger.debug(
"Skip rule because of failed condition, feature %s", key
)
continue
if rule.filters:
if _isFilteredOut(rule.filters, evalContext):
logger.debug(
"Skip rule because of filters/namespaces, feature %s", key
)
continue
if rule.force is not None:
if rule.is_force_rule:
# Only force rules check their condition here. An experiment rule's
# condition is checked in _run_experiment after the sticky bucket
# lookup, so a sticky-bucketed user stays in the experiment after
# the condition is narrowed (matches core.ts in the JS SDK).
if rule.condition and not evalCondition(
evalContext.user.attributes, rule.condition, evalContext.global_ctx.saved_groups
):
logger.debug(
"Skip rule because of failed condition, feature %s", key
)
continue
if not _isIncludedInRollout(
seed=rule.seed or key,
hashAttribute=rule.hashAttribute,
Expand Down
Loading
Loading