diff --git a/.sampo/changesets/local-eval-feature-enrollment.md b/.sampo/changesets/local-eval-feature-enrollment.md new file mode 100644 index 000000000..9c9f98fe6 --- /dev/null +++ b/.sampo/changesets/local-eval-feature-enrollment.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Local evaluation now respects early access feature enrollment. A flag with feature enrollment uses the `$feature_enrollment/` 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`. diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index cfe847fbb..6a16ee319 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -492,6 +492,21 @@ def match_feature_flag_properties( flag_filters = flag.get("filters") or {} + if flag_filters.get("feature_enrollment") is True: + enrollment_key = f"$feature_enrollment/{flag.get('key')}" + 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: + 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) diff --git a/posthog/test/test_feature_flags.py b/posthog/test/test_feature_flags.py index eb89117b1..ff1d56593 100644 --- a/posthog/test/test_feature_flags.py +++ b/posthog/test/test_feature_flags.py @@ -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):