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
5 changes: 5 additions & 0 deletions .sampo/changesets/local-eval-feature-enrollment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Every early access feature's flag has feature_enrollment: True, so a get_all_flags call without $feature_enrollment/<key> now makes a /flags request, and only_evaluate_locally=True drops those flags from the result. Should this be a minor bump with a "Behavior changes" note, like 7.79.0, that says to pass the property or use flag_keys?

---

Local evaluation now respects early access feature enrollment. A flag with feature enrollment uses the `$feature_enrollment/<flag key>` person property when you pass it, and otherwise falls back to the flags service. Before, it returned `False` for enrolled people who matched no release condition, while the flags service returned `True`.
15 changes: 15 additions & 0 deletions posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,21 @@ def match_feature_flag_properties(

flag_filters = flag.get("filters") or {}

if flag_filters.get("feature_enrollment") is True:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: When the call passes no groups, _compute_flag_locally returns False for a group flag before this function runs, so the new group check never fires and an enrolled person gets False where the flags service returns True. Could the group-enrollment check move into _compute_flag_locally, ahead of the group_name not in groups return?

enrollment_key = f"$feature_enrollment/{flag.get('key')}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: For a flag without a key, flag.get('key') builds $feature_enrollment/None and looks that up. I think this should use flag['key'], as get_matching_variant does, so a missing key raises a KeyError instead.

if flag_filters.get("aggregation_group_type_index") is not None:
raise InconclusiveMatchError(
"Group flag has feature enrollment, which reads person properties",
reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION,
)
if enrollment_key not in properties:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: This raises on a missing enrollment key while the flags service falls through to release conditions, and the reason is only in the PR description. I think a short comment here would help: the server reads the stored person property, which the SDK can't see.

raise InconclusiveMatchError(
f"Flag has feature enrollment and '{enrollment_key}' was not passed",
reason=UnresolvedFlagReason.MISSING_CONTEXT,
)
enrollment = properties[enrollment_key]
return enrollment is True or enrollment == "true"

# Holdouts are evaluated before release conditions, so a held-out value is excluded
# from the flag's targeting entirely rather than being bucketed into a variant.
holdout_variant = _get_holdout_variant(flag, bucketing_value)
Expand Down
72 changes: 72 additions & 0 deletions posthog/test/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,78 @@ def test_experience_continuity_flag_not_evaluated_locally(self, patch_flags):
)
self.assertEqual(patch_flags.call_count, 1)

@parameterized.expand(
[
("enrollment not passed", {}, 0, "flags-fallback-value", 1, False),
(
"enrolled",
{"$feature_enrollment/beta-feature": "true"},
0,
True,
0,
False,
),
(
"opted out",
{"$feature_enrollment/beta-feature": False},
100,
False,
0,
False,
),
(
"not a true value",
{"$feature_enrollment/beta-feature": 1},
100,
False,
0,
False,
),
(
"group flag",
{"$feature_enrollment/beta-feature": False},
100,
"flags-fallback-value",
1,
True,
),
]
)
def test_feature_enrollment_flag(
self, _name, person_properties, rollout, expected, flags_calls, group
):
client = Client(FAKE_TEST_API_KEY, secret_key="test")
client.feature_flags = [
{
"id": 1,
"key": "beta-feature",
"active": True,
"filters": {
"groups": [{"properties": [], "rollout_percentage": rollout}],
"feature_enrollment": True,
"aggregation_group_type_index": 0 if group else None,
},
}
]
client.group_type_mapping = {"0": "company"}
with mock.patch("posthog.client.flags") as patch_flags:
patch_flags.return_value = {
"featureFlags": {"beta-feature": "flags-fallback-value"}
}
value = client.get_feature_flag(
"beta-feature",
"distinct_id",
person_properties=person_properties,
groups={"company": "acme"} if group else None,
group_properties={
"company": {"$feature_enrollment/beta-feature": "true"}
}
if group
else None,
)

self.assertEqual((value, patch_flags.call_count), (expected, flags_calls))

@mock.patch.object(Client, "capture")
@mock.patch("posthog.client.flags")
def test_get_all_flags_with_fallback(self, patch_flags, patch_capture):
Expand Down
Loading