From 41e6d3de26047ac0bab38734b4185a3385c8f658 Mon Sep 17 00:00:00 2001 From: srednoff888-art Date: Sat, 21 Nov 2026 12:37:10 +0300 Subject: [PATCH 1/7] fix(flags): don't match person_metadata filters against person properties A cohort can filter on Person metadata > First seen (type "person_metadata", key "created_at"). /flags reads that value from the persons table and ignores any created_at person property. Local evaluation treated every filter type other than cohort and flag as a person property, so a caller passing created_at got an answer based on that value, which could disagree with /flags. Only person and group filters are now matched against the properties the caller passes. Any other type is unsupported_definition: the definition check flags it when definitions load, and evaluation falls back to /flags (or stays unresolved with only_evaluate_locally). match_property itself is unchanged. Part of PostHog/posthog#137503 (posthog-python) Co-Authored-By: Claude Opus 5.5 --- .sampo/changesets/person-metadata-filters.md | 5 + posthog/feature_flags.py | 19 ++++ .../test_feature_flags_person_metadata.py | 99 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 .sampo/changesets/person-metadata-filters.md create mode 100644 posthog/test/test_feature_flags_person_metadata.py diff --git a/.sampo/changesets/person-metadata-filters.md b/.sampo/changesets/person-metadata-filters.md new file mode 100644 index 000000000..934ceff51 --- /dev/null +++ b/.sampo/changesets/person-metadata-filters.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Local flag evaluation treats a filter type it cannot evaluate, such as a cohort's `person_metadata` filter on first seen, as unsupported and falls back to `/flags`, instead of matching it against a person property with the same key. diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 6a2e98693..9d92033b8 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -795,6 +795,23 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool: return enrollment is True or enrollment == "true" +# Filter types matched against the person or group properties the caller passes. Cohort +# and flag filters have their own matching. Any other type, such as "person_metadata" +# (a value /flags reads from the persons table, not from person properties), can't be +# evaluated locally: matching it against a caller property with the same key could +# disagree with /flags. +_PROPERTY_FILTER_TYPES = ("person", "group") + + +def _assert_property_filter_type_supported(prop) -> None: + property_type = prop.get("type") + if property_type and property_type not in _PROPERTY_FILTER_TYPES: + raise InconclusiveMatchError( + f"Can't evaluate a '{property_type}' filter on '{prop.get('key')}' locally", + reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, + ) + + def _match_condition_filter( prop, properties, @@ -829,6 +846,7 @@ def _match_condition_filter( device_id=device_id, property_matching_version=property_matching_version, ) + _assert_property_filter_type_supported(prop) return match_property(prop, properties, property_matching_version) @@ -1841,6 +1859,7 @@ def _is_unsupported_property( return _is_unsupported_dependency(prop, flags_by_key) if property_type != "cohort": try: + _assert_property_filter_type_supported(prop) _assert_filter_supported(prop) except InconclusiveMatchError: return True diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py new file mode 100644 index 000000000..e5973b1af --- /dev/null +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -0,0 +1,99 @@ +"""Local evaluation must not match `person_metadata` filters against person properties. + +A cohort can filter on Person metadata > First seen (`type: "person_metadata"`, key +`created_at`). /flags reads that value from the persons table and ignores a `created_at` +person property, so a caller passing `created_at` must get /flags' answer, not a local +one computed from that property. + +See https://github.com/PostHog/posthog/issues/137503 +""" + +import unittest +from unittest import mock + +from posthog.client import Client +from posthog.test.test_utils import FAKE_TEST_API_KEY + +FIRST_SEEN_FILTER = { + "key": "created_at", + "type": "person_metadata", + "operator": "is_date_after", + "value": "2025-01-01", +} + + +def _flag(properties): + return { + "id": 1, + "key": "first-seen-flag", + "active": True, + "filters": {"groups": [{"properties": properties, "rollout_percentage": 100}]}, + } + + +IN_COHORT = [{"key": "id", "value": 7, "type": "cohort"}] +FIRST_SEEN_COHORT = {"7": {"type": "AND", "values": [FIRST_SEEN_FILTER]}} + + +class TestPersonMetadataFilters(unittest.TestCase): + def _client(self, properties, cohorts=None): + client = Client(FAKE_TEST_API_KEY, secret_key=FAKE_TEST_API_KEY) + client.feature_flags = [_flag(properties)] + client.cohorts = cohorts or {} + return client + + @mock.patch("posthog.client.flags") + def test_cohort_filter_falls_back_to_flags_api(self, patch_flags): + # The person was created in 2020, so /flags answers False. + patch_flags.return_value = {"featureFlags": {"first-seen-flag": False}} + client = self._client(IN_COHORT, FIRST_SEEN_COHORT) + + result = client.get_feature_flag( + "first-seen-flag", + "user-123", + person_properties={"created_at": "2026-06-01"}, + ) + + self.assertFalse(result) + self.assertEqual(patch_flags.call_count, 1) + + @mock.patch("posthog.client.flags") + def test_condition_filter_falls_back_to_flags_api(self, patch_flags): + patch_flags.return_value = {"featureFlags": {"first-seen-flag": False}} + client = self._client([FIRST_SEEN_FILTER]) + + result = client.get_feature_flag( + "first-seen-flag", + "user-123", + person_properties={"created_at": "2026-06-01"}, + ) + + self.assertFalse(result) + self.assertEqual(patch_flags.call_count, 1) + + @mock.patch("posthog.client.flags") + def test_only_evaluate_locally_leaves_the_flag_unresolved(self, patch_flags): + client = self._client(IN_COHORT, FIRST_SEEN_COHORT) + + result = client.get_feature_flag( + "first-seen-flag", + "user-123", + person_properties={"created_at": "2026-06-01"}, + only_evaluate_locally=True, + ) + + self.assertIsNone(result) + self.assertEqual(patch_flags.call_count, 0) + + @mock.patch("posthog.client.flags") + def test_person_filter_on_created_at_is_still_local(self, patch_flags): + client = self._client([{**FIRST_SEEN_FILTER, "type": "person"}]) + + result = client.get_feature_flag( + "first-seen-flag", + "user-123", + person_properties={"created_at": "2026-06-01"}, + ) + + self.assertTrue(result) + self.assertEqual(patch_flags.call_count, 0) From 494a2c3f266a382c39037e4c993a15390ae058a6 Mon Sep 17 00:00:00 2001 From: srednoff888-art Date: Tue, 24 Nov 2026 09:48:54 +0300 Subject: [PATCH 2/7] fix(flags): send flags with an unsupported filter type to the server The error let later conditions decide the flag. Only /flags knows the value, so treat it like a static cohort. Also cover the load-time warning, evaluate_flags and negation, with a client that sends nothing. Co-Authored-By: Claude Opus 5.5 --- posthog/feature_flags.py | 7 +- .../test_feature_flags_person_metadata.py | 240 +++++++++++++++--- 2 files changed, 203 insertions(+), 44 deletions(-) diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 9d92033b8..53f2bee88 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -799,14 +799,15 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool: # and flag filters have their own matching. Any other type, such as "person_metadata" # (a value /flags reads from the persons table, not from person properties), can't be # evaluated locally: matching it against a caller property with the same key could -# disagree with /flags. +# disagree with /flags. No caller can supply the value, so like a static cohort it must +# not let a later condition decide the flag. _PROPERTY_FILTER_TYPES = ("person", "group") def _assert_property_filter_type_supported(prop) -> None: property_type = prop.get("type") if property_type and property_type not in _PROPERTY_FILTER_TYPES: - raise InconclusiveMatchError( + raise RequiresServerEvaluation( f"Can't evaluate a '{property_type}' filter on '{prop.get('key')}' locally", reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, ) @@ -1861,7 +1862,7 @@ def _is_unsupported_property( try: _assert_property_filter_type_supported(prop) _assert_filter_supported(prop) - except InconclusiveMatchError: + except (InconclusiveMatchError, RequiresServerEvaluation): return True return False cohort_id = str(prop.get("value")) diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index e5973b1af..2cff8a0db 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -8,11 +8,16 @@ See https://github.com/PostHog/posthog/issues/137503 """ +import logging import unittest from unittest import mock +from parameterized import parameterized + from posthog.client import Client +from posthog.request import GetResponse from posthog.test.test_utils import FAKE_TEST_API_KEY +from posthog.types import UnresolvedFlagReason FIRST_SEEN_FILTER = { "key": "created_at", @@ -20,80 +25,233 @@ "operator": "is_date_after", "value": "2025-01-01", } +IN_COHORT = [{"key": "id", "value": 7, "type": "cohort"}] +NOT_IN_COHORT = [{"key": "id", "value": 7, "type": "cohort", "operator": "not_in"}] +CREATED_IN_2026 = {"created_at": "2026-06-01"} +VARIANTS = { + "variants": [ + {"key": "new-user", "rollout_percentage": 50}, + {"key": "existing-user", "rollout_percentage": 50}, + ] +} + + +def _cohort(*filters): + return {"7": {"type": "AND", "values": list(filters)}} -def _flag(properties): +FIRST_SEEN_COHORT = _cohort(FIRST_SEEN_FILTER) + + +def _condition(properties, rollout_percentage=100, **kwargs): + return { + "properties": properties, + "rollout_percentage": rollout_percentage, + **kwargs, + } + + +def _flag(*conditions, key="first-seen-flag", **filters): return { "id": 1, - "key": "first-seen-flag", + "key": key, "active": True, - "filters": {"groups": [{"properties": properties, "rollout_percentage": 100}]}, + "filters": {"groups": list(conditions), **filters}, } -IN_COHORT = [{"key": "id", "value": 7, "type": "cohort"}] -FIRST_SEEN_COHORT = {"7": {"type": "AND", "values": [FIRST_SEEN_FILTER]}} +IN_COHORT_FLAG = _flag(_condition(IN_COHORT)) +IN_CONDITION_FLAG = _flag(_condition([FIRST_SEEN_FILTER])) +DEPENDS_ON_FIRST_SEEN = { + "key": "dependency", + "type": "flag", + "operator": "flag_evaluates_to", + "value": True, + "dependency_chain": ["dependency"], +} + +# Flags whose first condition only /flags can check, and what /flags answers. +SERVER_ONLY_FIRST_CONDITION = [ + ( + "another_variant", + [ + _flag( + _condition(IN_COHORT, variant="new-user"), + _condition([], variant="existing-user"), + key="target", + multivariate=VARIANTS, + ) + ], + "new-user", + ), + ( + "early_exit", + [ + _flag( + _condition(IN_COHORT, rollout_percentage=0), + _condition([]), + key="target", + early_exit=True, + ) + ], + False, + ), + ( + "dependency", + [ + _flag(_condition([FIRST_SEEN_FILTER]), key="dependency"), + _flag( + _condition([DEPENDS_ON_FIRST_SEEN], variant="new-user"), + _condition([], variant="existing-user"), + key="target", + multivariate=VARIANTS, + ), + ], + "new-user", + ), +] class TestPersonMetadataFilters(unittest.TestCase): - def _client(self, properties, cohorts=None): - client = Client(FAKE_TEST_API_KEY, secret_key=FAKE_TEST_API_KEY) - client.feature_flags = [_flag(properties)] - client.cohorts = cohorts or {} + def _client(self, flags, cohorts=FIRST_SEEN_COHORT, group_type_mapping=None): + client = Client( + FAKE_TEST_API_KEY, + # A key no other test uses, so a poller leaked by another module never matches it. + secret_key="test-person-metadata-filters", + enable_local_evaluation=False, + send=False, + ) + self.addCleanup(client.shutdown) + definitions = { + "flags": flags, + "group_type_mapping": group_type_mapping or {}, + "cohorts": cohorts, + } + response = GetResponse(data=definitions, etag=None, not_modified=False) + with mock.patch("posthog.client.get", return_value=response): + client._load_feature_flags() return client + @parameterized.expand( + [("cohort", IN_COHORT_FLAG), ("condition", IN_CONDITION_FLAG)] + ) @mock.patch("posthog.client.flags") - def test_cohort_filter_falls_back_to_flags_api(self, patch_flags): + def test_filter_falls_back_to_flags_api(self, _, flag, patch_flags): # The person was created in 2020, so /flags answers False. patch_flags.return_value = {"featureFlags": {"first-seen-flag": False}} - client = self._client(IN_COHORT, FIRST_SEEN_COHORT) + client = self._client([flag]) - result = client.get_feature_flag( - "first-seen-flag", - "user-123", - person_properties={"created_at": "2026-06-01"}, - ) + snapshot = client.evaluate_flags("user-123", person_properties=CREATED_IN_2026) - self.assertFalse(result) + self.assertIs(snapshot.get_flag("first-seen-flag"), False) self.assertEqual(patch_flags.call_count, 1) + @parameterized.expand( + [ + ("cohort", IN_COHORT_FLAG, FIRST_SEEN_COHORT), + ("condition", IN_CONDITION_FLAG, {}), + ("not_in_cohort", _flag(_condition(NOT_IN_COHORT)), FIRST_SEEN_COHORT), + ( + "negated_cohort_leaf", + IN_COHORT_FLAG, + _cohort({**FIRST_SEEN_FILTER, "negation": True}), + ), + ] + ) @mock.patch("posthog.client.flags") - def test_condition_filter_falls_back_to_flags_api(self, patch_flags): - patch_flags.return_value = {"featureFlags": {"first-seen-flag": False}} - client = self._client([FIRST_SEEN_FILTER]) + def test_only_evaluate_locally_leaves_the_flag_unresolved( + self, _, flag, cohorts, patch_flags + ): + client = self._client([flag], cohorts) - result = client.get_feature_flag( - "first-seen-flag", - "user-123", - person_properties={"created_at": "2026-06-01"}, + for person_properties in (CREATED_IN_2026, {}): + snapshot = client.evaluate_flags( + "user-123", + person_properties=person_properties, + only_evaluate_locally=True, + ) + self.assertIsNone(snapshot.get_flag("first-seen-flag")) + self.assertEqual( + dict(snapshot.unresolved_flags), + {"first-seen-flag": UnresolvedFlagReason.UNSUPPORTED_DEFINITION}, + ) + self.assertEqual(patch_flags.call_count, 0) + + @parameterized.expand( + [("cohort", IN_COHORT_FLAG), ("condition", IN_CONDITION_FLAG)] + ) + def test_loading_the_definition_logs_a_warning(self, _, flag): + with self.assertLogs("posthog", level=logging.WARNING) as logs: + self._client([flag]) + + self.assertTrue( + any( + "first-seen-flag" in record.getMessage() + and "(reason: unsupported_definition)" in record.getMessage() + for record in logs.records + ) ) - self.assertFalse(result) - self.assertEqual(patch_flags.call_count, 1) + @parameterized.expand(SERVER_ONLY_FIRST_CONDITION) + @mock.patch("posthog.client.flags") + def test_later_condition_does_not_decide_the_flag( + self, _, flags, remote, patch_flags + ): + patch_flags.return_value = {"featureFlags": {"target": remote}} + client = self._client(flags) + + for person_properties in (CREATED_IN_2026, {}): + patch_flags.reset_mock() + snapshot = client.evaluate_flags( + "user-123", person_properties=person_properties, flag_keys=["target"] + ) + self.assertEqual(snapshot.get_flag("target"), remote) + self.assertEqual(patch_flags.call_count, 1) @mock.patch("posthog.client.flags") - def test_only_evaluate_locally_leaves_the_flag_unresolved(self, patch_flags): - client = self._client(IN_COHORT, FIRST_SEEN_COHORT) + def test_another_filter_still_rules_the_condition_out(self, patch_flags): + plan = {"key": "plan", "type": "person", "operator": "exact", "value": "paid"} + client = self._client([_flag(_condition([FIRST_SEEN_FILTER, plan]))]) - result = client.get_feature_flag( - "first-seen-flag", - "user-123", - person_properties={"created_at": "2026-06-01"}, - only_evaluate_locally=True, - ) + snapshot = client.evaluate_flags("user-123", person_properties={"plan": "free"}) + + self.assertIs(snapshot.get_flag("first-seen-flag"), False) + self.assertEqual(patch_flags.call_count, 0) - self.assertIsNone(result) + @parameterized.expand([("person", {"type": "person"}), ("untyped", {})]) + @mock.patch("posthog.client.flags") + def test_person_filter_on_created_at_is_still_local( + self, _, filter_type, patch_flags + ): + prop = {**FIRST_SEEN_FILTER, **filter_type} + if not filter_type: + del prop["type"] + client = self._client([_flag(_condition([prop]))]) + + snapshot = client.evaluate_flags("user-123", person_properties=CREATED_IN_2026) + + self.assertIs(snapshot.get_flag("first-seen-flag"), True) self.assertEqual(patch_flags.call_count, 0) @mock.patch("posthog.client.flags") - def test_person_filter_on_created_at_is_still_local(self, patch_flags): - client = self._client([{**FIRST_SEEN_FILTER, "type": "person"}]) + def test_group_filter_is_still_local(self, patch_flags): + prop = { + "key": "plan", + "type": "group", + "group_type_index": 0, + "operator": "exact", + "value": "paid", + } + client = self._client( + [_flag(_condition([prop]), aggregation_group_type_index=0)], + group_type_mapping={"0": "company"}, + ) - result = client.get_feature_flag( - "first-seen-flag", + snapshot = client.evaluate_flags( "user-123", - person_properties={"created_at": "2026-06-01"}, + groups={"company": "acme"}, + group_properties={"company": {"plan": "paid"}}, ) - self.assertTrue(result) + self.assertIs(snapshot.get_flag("first-seen-flag"), True) self.assertEqual(patch_flags.call_count, 0) From 07ee9f6cadf11cf55f506c6de8796ffa9d4f3c8c Mon Sep 17 00:00:00 2001 From: srednoff888-art Date: Tue, 24 Nov 2026 23:29:12 +0300 Subject: [PATCH 3/7] fix(flags): let another filter decide a cohort with a server-only filter A cohort stopped at the first filter that only the server can check, even when another filter had already decided it. Raise the error only when the cohort stays undecided. Co-Authored-By: Claude Opus 5.5 --- posthog/client.py | 4 +- posthog/feature_flags.py | 24 +++++-- .../test_feature_flags_person_metadata.py | 69 +++++++++++++++++-- posthog/types.py | 6 +- 4 files changed, 89 insertions(+), 14 deletions(-) diff --git a/posthog/client.py b/posthog/client.py index 7d4de2c8a..717b158b1 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -4279,8 +4279,8 @@ def _log_unresolvable_flags( self.log.warning( f"[FEATURE FLAGS] {len(unsupported_definition_keys)} flag(s) use " "something local evaluation doesn't support, such as a static " - "cohort, an unknown operator, a malformed value, or a missing or " - "circular flag dependency " + "cohort, an unknown operator or filter type, a malformed value, or a " + "missing or circular flag dependency " f"(reason: {UnresolvedFlagReason.UNSUPPORTED_DEFINITION.value}): " f"{', '.join(unsupported_definition_keys)}. Evaluating them makes a " "remote request when evaluation reaches that part, or leaves them " diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 53f2bee88..9787d9ffc 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -133,6 +133,14 @@ def __init__(self, *args, reason: Optional[UnresolvedFlagReason] = None): self.reason = reason +class _RequiresServerFilter(RequiresServerEvaluation): + """ + Raised for a single filter that only the server can check. Like any + RequiresServerEvaluation it stops later conditions from deciding the flag, but in a + cohort another filter can still make the result certain. + """ + + # When several causes apply to one flag, the spec reports the first reason in this # order, so every SDK picks the same reason whatever order it inspects filters in. _UNRESOLVED_REASON_PRECEDENCE = ( @@ -458,8 +466,9 @@ def _evaluate_dependency( except RequiresServerEvaluation as e: # The dependent flag needs the server too, so its later conditions must # not resolve it locally. Nothing is cached, so another filter on this - # dependency also raises this error. - raise RequiresServerEvaluation( + # dependency also raises this error. The type is kept, so a cohort treats the + # dependency like the filter that stopped it. + raise type(e)( f"Cannot evaluate flag dependency '{dep_flag_key}': {e}", reason=UnresolvedFlagReason.UNRESOLVED_DEPENDENCY, ) from e @@ -799,15 +808,15 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool: # and flag filters have their own matching. Any other type, such as "person_metadata" # (a value /flags reads from the persons table, not from person properties), can't be # evaluated locally: matching it against a caller property with the same key could -# disagree with /flags. No caller can supply the value, so like a static cohort it must -# not let a later condition decide the flag. +# disagree with /flags. No caller can supply the value, so a later condition must not +# decide the flag either. _PROPERTY_FILTER_TYPES = ("person", "group") def _assert_property_filter_type_supported(prop) -> None: property_type = prop.get("type") if property_type and property_type not in _PROPERTY_FILTER_TYPES: - raise RequiresServerEvaluation( + raise _RequiresServerFilter( f"Can't evaluate a '{property_type}' filter on '{prop.get('key')}' locally", reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, ) @@ -1734,6 +1743,7 @@ def match_property_group( decisive_result = None inconclusive_reason: Optional[UnresolvedFlagReason] = None + server_filter_error: Optional[_RequiresServerFilter] = None for prop in properties: try: @@ -1749,6 +1759,8 @@ def match_property_group( ) if effective_match != is_and: decisive_result = effective_match + except _RequiresServerFilter as e: + server_filter_error = server_filter_error or e except RequiresServerEvaluation: # Static/missing cohorts and malformed definitions always require server evaluation, # even when another branch would otherwise resolve the group locally. @@ -1759,6 +1771,8 @@ def match_property_group( if decisive_result is not None: return decisive_result + if server_filter_error is not None: + raise server_filter_error if inconclusive_reason is not None: raise InconclusiveMatchError( "Can't match cohort without a given cohort property value", diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index 2cff8a0db..830368e5a 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -28,6 +28,7 @@ IN_COHORT = [{"key": "id", "value": 7, "type": "cohort"}] NOT_IN_COHORT = [{"key": "id", "value": 7, "type": "cohort", "operator": "not_in"}] CREATED_IN_2026 = {"created_at": "2026-06-01"} +PLAN_PAID = {"key": "plan", "type": "person", "operator": "exact", "value": "paid"} VARIANTS = { "variants": [ {"key": "new-user", "rollout_percentage": 50}, @@ -36,8 +37,8 @@ } -def _cohort(*filters): - return {"7": {"type": "AND", "values": list(filters)}} +def _cohort(*filters, group_type="AND"): + return {"7": {"type": group_type, "values": list(filters)}} FIRST_SEEN_COHORT = _cohort(FIRST_SEEN_FILTER) @@ -208,10 +209,70 @@ def test_later_condition_does_not_decide_the_flag( self.assertEqual(snapshot.get_flag("target"), remote) self.assertEqual(patch_flags.call_count, 1) + @parameterized.expand( + [ + ("and", "AND", IN_COHORT, "free", False), + ("and_not_in", "AND", NOT_IN_COHORT, "free", True), + ("or", "OR", IN_COHORT, "paid", True), + ("or_not_in", "OR", NOT_IN_COHORT, "paid", False), + ] + ) + @mock.patch("posthog.client.flags") + def test_another_filter_still_decides_the_cohort( + self, _, group_type, flag_properties, plan, expected, patch_flags + ): + dependency = _flag(_condition([FIRST_SEEN_FILTER]), key="dependency") + for filters in ( + (PLAN_PAID, FIRST_SEEN_FILTER), + (FIRST_SEEN_FILTER, PLAN_PAID), + ({"type": "AND", "values": [FIRST_SEEN_FILTER]}, PLAN_PAID), + (DEPENDS_ON_FIRST_SEEN, PLAN_PAID), + ): + client = self._client( + [_flag(_condition(flag_properties)), dependency], + _cohort(*filters, group_type=group_type), + ) + for created_at in (CREATED_IN_2026, {}): + with self.subTest(filters=filters, created_at=created_at): + snapshot = client.evaluate_flags( + "user-123", + person_properties={"plan": plan, **created_at}, + flag_keys=["first-seen-flag"], + ) + self.assertIs(snapshot.get_flag("first-seen-flag"), expected) + self.assertEqual(patch_flags.call_count, 0) + + @parameterized.expand( + [ + ("and", "AND", "paid"), + ("or", "OR", "free"), + ] + ) + @mock.patch("posthog.client.flags") + def test_undecided_cohort_blocks_later_conditions( + self, _, group_type, plan, patch_flags + ): + patch_flags.return_value = {"featureFlags": {"target": "new-user"}} + for filters in ( + (PLAN_PAID, FIRST_SEEN_FILTER), + (FIRST_SEEN_FILTER, PLAN_PAID), + ): + patch_flags.reset_mock() + client = self._client( + SERVER_ONLY_FIRST_CONDITION[0][1], + _cohort(*filters, group_type=group_type), + ) + + snapshot = client.evaluate_flags( + "user-123", person_properties={"plan": plan}, flag_keys=["target"] + ) + + self.assertEqual(snapshot.get_flag("target"), "new-user") + self.assertEqual(patch_flags.call_count, 1) + @mock.patch("posthog.client.flags") def test_another_filter_still_rules_the_condition_out(self, patch_flags): - plan = {"key": "plan", "type": "person", "operator": "exact", "value": "paid"} - client = self._client([_flag(_condition([FIRST_SEEN_FILTER, plan]))]) + client = self._client([_flag(_condition([FIRST_SEEN_FILTER, PLAN_PAID]))]) snapshot = client.evaluate_flags("user-123", person_properties={"plan": "free"}) diff --git a/posthog/types.py b/posthog/types.py index 9335274e5..541d7c5ba 100644 --- a/posthog/types.py +++ b/posthog/types.py @@ -128,9 +128,9 @@ class UnresolvedFlagReason(str, Enum): EXPERIENCE_CONTINUITY: The flag has experience continuity enabled, which local evaluation never resolves. Change the flag to fix it. UNSUPPORTED_DEFINITION: The flag uses something the local evaluator does not - support, such as a static cohort, an unknown operator, a malformed value, - a dependency with no loaded definition or a circular dependency. Change - the flag or upgrade the SDK to fix it. + support, such as a static cohort, an unknown operator or filter type, a + malformed value, a dependency with no loaded definition or a circular + dependency. Change the flag or upgrade the SDK to fix it. UNRESOLVED_DEPENDENCY: A flag this flag depends on has a loaded definition but was unresolved. MISSING_CONTEXT: The call did not pass a property, group property or device From 19000d370fbf104568f45ee163aa1fe1bbd33820 Mon Sep 17 00:00:00 2001 From: srednoff888-art Date: Wed, 25 Nov 2026 01:22:37 +0300 Subject: [PATCH 4/7] fix(flags): report the highest-precedence reason for a server-only filter An error that only the server can resolve kept its own reason, so a flag with a runtime unsupported_definition filter could be reported as unresolved_dependency. The reasons are now merged in the cohort group, in the condition and across conditions. Co-Authored-By: Claude Opus 5.5 --- posthog/feature_flags.py | 9 +- .../test_feature_flags_person_metadata.py | 148 +++++++++++++++++- 2 files changed, 155 insertions(+), 2 deletions(-) diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 9787d9ffc..8837489e7 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -638,6 +638,7 @@ def _match_flag_conditions( cohort_properties, group_type_mapping, ) + e.reason = _pick_unresolved_reason(inconclusive_reason, e.reason) raise except InconclusiveMatchError as e: # Evaluation error (bad regex, invalid date, missing property, etc.) @@ -958,6 +959,7 @@ def _raise_condition_errors(feature_flag, server_evaluation_error, inconclusive_ # Re-raise the original error type: match_feature_flag_properties stops at it # and falls back to the server instead of trying later conditions. if server_evaluation_error is not None: + server_evaluation_error.reason = inconclusive_reason raise server_evaluation_error if inconclusive_reason is not None: raise InconclusiveMatchError( @@ -1761,9 +1763,11 @@ def match_property_group( decisive_result = effective_match except _RequiresServerFilter as e: server_filter_error = server_filter_error or e - except RequiresServerEvaluation: + inconclusive_reason = _pick_unresolved_reason(inconclusive_reason, e.reason) + except RequiresServerEvaluation as e: # Static/missing cohorts and malformed definitions always require server evaluation, # even when another branch would otherwise resolve the group locally. + e.reason = _pick_unresolved_reason(inconclusive_reason, e.reason) raise except InconclusiveMatchError as e: log.debug(f"Failed to compute property {prop} locally: {e}") @@ -1772,6 +1776,9 @@ def match_property_group( if decisive_result is not None: return decisive_result if server_filter_error is not None: + server_filter_error.reason = _pick_unresolved_reason( + inconclusive_reason, server_filter_error.reason + ) raise server_filter_error if inconclusive_reason is not None: raise InconclusiveMatchError( diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index 830368e5a..9089b821b 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -15,6 +15,7 @@ from parameterized import parameterized from posthog.client import Client +from posthog.feature_flags import RequiresServerEvaluation, match_property_group from posthog.request import GetResponse from posthog.test.test_utils import FAKE_TEST_API_KEY from posthog.types import UnresolvedFlagReason @@ -71,6 +72,16 @@ def _flag(*conditions, key="first-seen-flag", **filters): "dependency_chain": ["dependency"], } +# The regex engines of Python and /flags disagree on this filter for "foo\n", so it is +# unsupported for that value. The definition alone doesn't tell. +UNSUPPORTED_REGEX = { + "key": "name", + "type": "person", + "operator": "regex", + "value": "foo.*$", +} +IN_STATIC_COHORT = {"key": "id", "value": 404, "type": "cohort"} + # Flags whose first condition only /flags can check, and what /flags answers. SERVER_ONLY_FIRST_CONDITION = [ ( @@ -152,6 +163,11 @@ def test_filter_falls_back_to_flags_api(self, _, flag, patch_flags): ("cohort", IN_COHORT_FLAG, FIRST_SEEN_COHORT), ("condition", IN_CONDITION_FLAG, {}), ("not_in_cohort", _flag(_condition(NOT_IN_COHORT)), FIRST_SEEN_COHORT), + ( + "another_unsupported_type", + _flag(_condition([{**FIRST_SEEN_FILTER, "type": "behavioral"}])), + {}, + ), ( "negated_cohort_leaf", IN_COHORT_FLAG, @@ -227,10 +243,14 @@ def test_another_filter_still_decides_the_cohort( (FIRST_SEEN_FILTER, PLAN_PAID), ({"type": "AND", "values": [FIRST_SEEN_FILTER]}, PLAN_PAID), (DEPENDS_ON_FIRST_SEEN, PLAN_PAID), + ({"key": "id", "value": 8, "type": "cohort"}, PLAN_PAID), ): client = self._client( [_flag(_condition(flag_properties)), dependency], - _cohort(*filters, group_type=group_type), + { + **_cohort(*filters, group_type=group_type), + "8": {"type": "AND", "values": [FIRST_SEEN_FILTER]}, + }, ) for created_at in (CREATED_IN_2026, {}): with self.subTest(filters=filters, created_at=created_at): @@ -238,10 +258,136 @@ def test_another_filter_still_decides_the_cohort( "user-123", person_properties={"plan": plan, **created_at}, flag_keys=["first-seen-flag"], + only_evaluate_locally=True, ) self.assertIs(snapshot.get_flag("first-seen-flag"), expected) + self.assertEqual(dict(snapshot.unresolved_flags), {}) self.assertEqual(patch_flags.call_count, 0) + @parameterized.expand( + [ + ("and", "AND", IN_COHORT, "paid"), + ("and_not_in", "AND", NOT_IN_COHORT, "paid"), + ("or", "OR", IN_COHORT, "free"), + ("or_not_in", "OR", NOT_IN_COHORT, "free"), + ] + ) + def test_undecided_cohort_is_unsupported( + self, _, group_type, flag_properties, plan + ): + for filters in ( + (PLAN_PAID, FIRST_SEEN_FILTER), + (FIRST_SEEN_FILTER, PLAN_PAID), + ): + client = self._client( + [_flag(_condition(flag_properties))], + _cohort(*filters, group_type=group_type), + ) + + snapshot = client.evaluate_flags( + "user-123", + person_properties={"plan": plan}, + only_evaluate_locally=True, + ) + + self.assertEqual( + dict(snapshot.unresolved_flags), + {"first-seen-flag": UnresolvedFlagReason.UNSUPPORTED_DEFINITION}, + ) + + @parameterized.expand( + [ + ( + "cohort", + [_flag(_condition(IN_COHORT), key="target")], + _cohort(DEPENDS_ON_FIRST_SEEN, UNSUPPORTED_REGEX), + ), + ( + "cohort_other_order", + [_flag(_condition(IN_COHORT), key="target")], + _cohort(UNSUPPORTED_REGEX, DEPENDS_ON_FIRST_SEEN), + ), + ( + "condition", + [ + _flag( + _condition([DEPENDS_ON_FIRST_SEEN, UNSUPPORTED_REGEX]), + key="target", + ) + ], + {}, + ), + ( + "earlier_condition", + [ + _flag( + _condition([UNSUPPORTED_REGEX]), + _condition([DEPENDS_ON_FIRST_SEEN]), + key="target", + ) + ], + {}, + ), + ] + ) + def test_the_highest_precedence_reason_is_reported(self, _, flags, cohorts): + dependency = _flag(_condition([FIRST_SEEN_FILTER]), key="dependency") + client = self._client([*flags, dependency], cohorts) + + snapshot = client.evaluate_flags( + "user-123", + person_properties={"name": "foo\n"}, + flag_keys=["target"], + only_evaluate_locally=True, + ) + + self.assertEqual( + dict(snapshot.unresolved_flags), + {"target": UnresolvedFlagReason.UNSUPPORTED_DEFINITION}, + ) + + def test_a_cohort_that_stops_early_keeps_the_higher_reason(self): + # A dependency on a static cohort ends the cohort at once, whatever comes after it. + flags_by_key = { + "dependency": _flag(_condition([IN_STATIC_COHORT]), key="dependency") + } + with self.assertRaises(RequiresServerEvaluation) as raised: + match_property_group( + {"type": "AND", "values": [UNSUPPORTED_REGEX, DEPENDS_ON_FIRST_SEEN]}, + {"name": "foo\n"}, + {}, + flags_by_key=flags_by_key, + evaluation_cache={}, + distinct_id="user-123", + ) + self.assertEqual( + raised.exception.reason, UnresolvedFlagReason.UNSUPPORTED_DEFINITION + ) + + @parameterized.expand( + [ + ("server_filters", [DEPENDS_ON_FIRST_SEEN, FIRST_SEEN_FILTER]), + ("inconclusive_filter", [DEPENDS_ON_FIRST_SEEN, UNSUPPORTED_REGEX]), + ] + ) + def test_the_reason_does_not_depend_on_filter_order(self, _, filters): + flags_by_key = { + "dependency": _flag(_condition([FIRST_SEEN_FILTER]), key="dependency") + } + for ordered in (filters, filters[::-1]): + with self.assertRaises(RequiresServerEvaluation) as raised: + match_property_group( + {"type": "AND", "values": ordered}, + {"name": "foo\n"}, + {}, + flags_by_key=flags_by_key, + evaluation_cache={}, + distinct_id="user-123", + ) + self.assertEqual( + raised.exception.reason, UnresolvedFlagReason.UNSUPPORTED_DEFINITION + ) + @parameterized.expand( [ ("and", "AND", "paid"), From 3d709c706bcb072409e0dc854a60c385d5dca846 Mon Sep 17 00:00:00 2001 From: srednoff888-art Date: Wed, 25 Nov 2026 22:41:31 +0300 Subject: [PATCH 5/7] fix(flags): treat an empty filter type as unsupported Only a filter without a type is a person filter (PostHog/sdk-specs#251). Co-Authored-By: Claude Opus 5.5 --- posthog/feature_flags.py | 6 +++--- posthog/test/test_feature_flags_person_metadata.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index 8837489e7..aaa94c757 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -815,10 +815,10 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool: def _assert_property_filter_type_supported(prop) -> None: - property_type = prop.get("type") - if property_type and property_type not in _PROPERTY_FILTER_TYPES: + # Only a filter without a type is a person filter. An empty type is unsupported. + if "type" in prop and prop["type"] not in _PROPERTY_FILTER_TYPES: raise _RequiresServerFilter( - f"Can't evaluate a '{property_type}' filter on '{prop.get('key')}' locally", + f"Can't evaluate a '{prop['type']}' filter on '{prop.get('key')}' locally", reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, ) diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index 9089b821b..2158ea236 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -168,6 +168,16 @@ def test_filter_falls_back_to_flags_api(self, _, flag, patch_flags): _flag(_condition([{**FIRST_SEEN_FILTER, "type": "behavioral"}])), {}, ), + ( + "empty_type", + _flag(_condition([{**FIRST_SEEN_FILTER, "type": ""}])), + {}, + ), + ( + "empty_type_cohort_leaf", + IN_COHORT_FLAG, + _cohort({**FIRST_SEEN_FILTER, "type": ""}), + ), ( "negated_cohort_leaf", IN_COHORT_FLAG, From f1a521e55845cbe1a875f31ac7787d8646e82767 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 26 Nov 2026 09:51:46 +0100 Subject: [PATCH 6/7] fix(flags): follow the unsupported filter type contract from sdk-specs#251 --- .sampo/changesets/person-metadata-filters.md | 2 +- posthog/feature_flags.py | 34 ++----- .../test_feature_flags_person_metadata.py | 93 ++++++++++--------- 3 files changed, 58 insertions(+), 71 deletions(-) diff --git a/.sampo/changesets/person-metadata-filters.md b/.sampo/changesets/person-metadata-filters.md index 934ceff51..0b3649c7b 100644 --- a/.sampo/changesets/person-metadata-filters.md +++ b/.sampo/changesets/person-metadata-filters.md @@ -2,4 +2,4 @@ pypi/posthog: patch --- -Local flag evaluation treats a filter type it cannot evaluate, such as a cohort's `person_metadata` filter on first seen, as unsupported and falls back to `/flags`, instead of matching it against a person property with the same key. +Local flag evaluation treats a filter type it cannot evaluate, such as a cohort's `person_metadata` filter on first seen, as inconclusive instead of matching it against a person property with the same key. A later condition can still decide the flag, and otherwise it falls back to `/flags`. diff --git a/posthog/feature_flags.py b/posthog/feature_flags.py index aaa94c757..8e76cdbae 100644 --- a/posthog/feature_flags.py +++ b/posthog/feature_flags.py @@ -133,14 +133,6 @@ def __init__(self, *args, reason: Optional[UnresolvedFlagReason] = None): self.reason = reason -class _RequiresServerFilter(RequiresServerEvaluation): - """ - Raised for a single filter that only the server can check. Like any - RequiresServerEvaluation it stops later conditions from deciding the flag, but in a - cohort another filter can still make the result certain. - """ - - # When several causes apply to one flag, the spec reports the first reason in this # order, so every SDK picks the same reason whatever order it inspects filters in. _UNRESOLVED_REASON_PRECEDENCE = ( @@ -466,9 +458,8 @@ def _evaluate_dependency( except RequiresServerEvaluation as e: # The dependent flag needs the server too, so its later conditions must # not resolve it locally. Nothing is cached, so another filter on this - # dependency also raises this error. The type is kept, so a cohort treats the - # dependency like the filter that stopped it. - raise type(e)( + # dependency also raises this error. + raise RequiresServerEvaluation( f"Cannot evaluate flag dependency '{dep_flag_key}': {e}", reason=UnresolvedFlagReason.UNRESOLVED_DEPENDENCY, ) from e @@ -809,15 +800,13 @@ def _match_feature_enrollment(flag, flag_filters, properties) -> bool: # and flag filters have their own matching. Any other type, such as "person_metadata" # (a value /flags reads from the persons table, not from person properties), can't be # evaluated locally: matching it against a caller property with the same key could -# disagree with /flags. No caller can supply the value, so a later condition must not -# decide the flag either. -_PROPERTY_FILTER_TYPES = ("person", "group") +# disagree with /flags. A missing, null, or empty type is a person filter. +_PROPERTY_FILTER_TYPES = (None, "", "person", "group") def _assert_property_filter_type_supported(prop) -> None: - # Only a filter without a type is a person filter. An empty type is unsupported. - if "type" in prop and prop["type"] not in _PROPERTY_FILTER_TYPES: - raise _RequiresServerFilter( + if prop.get("type") not in _PROPERTY_FILTER_TYPES: + raise InconclusiveMatchError( f"Can't evaluate a '{prop['type']}' filter on '{prop.get('key')}' locally", reason=UnresolvedFlagReason.UNSUPPORTED_DEFINITION, ) @@ -1745,7 +1734,6 @@ def match_property_group( decisive_result = None inconclusive_reason: Optional[UnresolvedFlagReason] = None - server_filter_error: Optional[_RequiresServerFilter] = None for prop in properties: try: @@ -1761,9 +1749,6 @@ def match_property_group( ) if effective_match != is_and: decisive_result = effective_match - except _RequiresServerFilter as e: - server_filter_error = server_filter_error or e - inconclusive_reason = _pick_unresolved_reason(inconclusive_reason, e.reason) except RequiresServerEvaluation as e: # Static/missing cohorts and malformed definitions always require server evaluation, # even when another branch would otherwise resolve the group locally. @@ -1775,11 +1760,6 @@ def match_property_group( if decisive_result is not None: return decisive_result - if server_filter_error is not None: - server_filter_error.reason = _pick_unresolved_reason( - inconclusive_reason, server_filter_error.reason - ) - raise server_filter_error if inconclusive_reason is not None: raise InconclusiveMatchError( "Can't match cohort without a given cohort property value", @@ -1883,7 +1863,7 @@ def _is_unsupported_property( try: _assert_property_filter_type_supported(prop) _assert_filter_supported(prop) - except (InconclusiveMatchError, RequiresServerEvaluation): + except InconclusiveMatchError: return True return False cohort_id = str(prop.get("value")) diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index 2158ea236..6e2973706 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -15,7 +15,11 @@ from parameterized import parameterized from posthog.client import Client -from posthog.feature_flags import RequiresServerEvaluation, match_property_group +from posthog.feature_flags import ( + InconclusiveMatchError, + RequiresServerEvaluation, + match_property_group, +) from posthog.request import GetResponse from posthog.test.test_utils import FAKE_TEST_API_KEY from posthog.types import UnresolvedFlagReason @@ -82,8 +86,8 @@ def _flag(*conditions, key="first-seen-flag", **filters): } IN_STATIC_COHORT = {"key": "id", "value": 404, "type": "cohort"} -# Flags whose first condition only /flags can check, and what /flags answers. -SERVER_ONLY_FIRST_CONDITION = [ +# Flags whose first condition can't be checked locally, followed by a catch-all. +LATER_CONDITION_DECIDES = [ ( "another_variant", [ @@ -94,19 +98,7 @@ def _flag(*conditions, key="first-seen-flag", **filters): multivariate=VARIANTS, ) ], - "new-user", - ), - ( - "early_exit", - [ - _flag( - _condition(IN_COHORT, rollout_percentage=0), - _condition([]), - key="target", - early_exit=True, - ) - ], - False, + "existing-user", ), ( "dependency", @@ -119,7 +111,7 @@ def _flag(*conditions, key="first-seen-flag", **filters): multivariate=VARIANTS, ), ], - "new-user", + "existing-user", ), ] @@ -168,16 +160,6 @@ def test_filter_falls_back_to_flags_api(self, _, flag, patch_flags): _flag(_condition([{**FIRST_SEEN_FILTER, "type": "behavioral"}])), {}, ), - ( - "empty_type", - _flag(_condition([{**FIRST_SEEN_FILTER, "type": ""}])), - {}, - ), - ( - "empty_type_cohort_leaf", - IN_COHORT_FLAG, - _cohort({**FIRST_SEEN_FILTER, "type": ""}), - ), ( "negated_cohort_leaf", IN_COHORT_FLAG, @@ -219,21 +201,41 @@ def test_loading_the_definition_logs_a_warning(self, _, flag): ) ) - @parameterized.expand(SERVER_ONLY_FIRST_CONDITION) + @parameterized.expand(LATER_CONDITION_DECIDES) @mock.patch("posthog.client.flags") - def test_later_condition_does_not_decide_the_flag( - self, _, flags, remote, patch_flags - ): - patch_flags.return_value = {"featureFlags": {"target": remote}} + def test_later_condition_decides_the_flag(self, _, flags, expected, patch_flags): client = self._client(flags) for person_properties in (CREATED_IN_2026, {}): - patch_flags.reset_mock() snapshot = client.evaluate_flags( "user-123", person_properties=person_properties, flag_keys=["target"] ) - self.assertEqual(snapshot.get_flag("target"), remote) - self.assertEqual(patch_flags.call_count, 1) + self.assertEqual(snapshot.get_flag("target"), expected) + self.assertEqual(patch_flags.call_count, 0) + + @mock.patch("posthog.client.flags") + def test_early_exit_after_unsupported_condition_stays_inconclusive( + self, patch_flags + ): + patch_flags.return_value = {"featureFlags": {"target": True}} + client = self._client( + [ + _flag( + _condition(IN_COHORT), + _condition([], rollout_percentage=0), + _condition([]), + key="target", + early_exit=True, + ) + ] + ) + + snapshot = client.evaluate_flags( + "user-123", person_properties=CREATED_IN_2026, flag_keys=["target"] + ) + + self.assertIs(snapshot.get_flag("target"), True) + self.assertEqual(patch_flags.call_count, 1) @parameterized.expand( [ @@ -385,7 +387,7 @@ def test_the_reason_does_not_depend_on_filter_order(self, _, filters): "dependency": _flag(_condition([FIRST_SEEN_FILTER]), key="dependency") } for ordered in (filters, filters[::-1]): - with self.assertRaises(RequiresServerEvaluation) as raised: + with self.assertRaises(InconclusiveMatchError) as raised: match_property_group( {"type": "AND", "values": ordered}, {"name": "foo\n"}, @@ -405,17 +407,15 @@ def test_the_reason_does_not_depend_on_filter_order(self, _, filters): ] ) @mock.patch("posthog.client.flags") - def test_undecided_cohort_blocks_later_conditions( + def test_undecided_cohort_lets_a_later_condition_decide( self, _, group_type, plan, patch_flags ): - patch_flags.return_value = {"featureFlags": {"target": "new-user"}} for filters in ( (PLAN_PAID, FIRST_SEEN_FILTER), (FIRST_SEEN_FILTER, PLAN_PAID), ): - patch_flags.reset_mock() client = self._client( - SERVER_ONLY_FIRST_CONDITION[0][1], + LATER_CONDITION_DECIDES[0][1], _cohort(*filters, group_type=group_type), ) @@ -423,8 +423,8 @@ def test_undecided_cohort_blocks_later_conditions( "user-123", person_properties={"plan": plan}, flag_keys=["target"] ) - self.assertEqual(snapshot.get_flag("target"), "new-user") - self.assertEqual(patch_flags.call_count, 1) + self.assertEqual(snapshot.get_flag("target"), "existing-user") + self.assertEqual(patch_flags.call_count, 0) @mock.patch("posthog.client.flags") def test_another_filter_still_rules_the_condition_out(self, patch_flags): @@ -435,7 +435,14 @@ def test_another_filter_still_rules_the_condition_out(self, patch_flags): self.assertIs(snapshot.get_flag("first-seen-flag"), False) self.assertEqual(patch_flags.call_count, 0) - @parameterized.expand([("person", {"type": "person"}), ("untyped", {})]) + @parameterized.expand( + [ + ("person", {"type": "person"}), + ("untyped", {}), + ("null_type", {"type": None}), + ("empty_type", {"type": ""}), + ] + ) @mock.patch("posthog.client.flags") def test_person_filter_on_created_at_is_still_local( self, _, filter_type, patch_flags From d0ab027591ae9dfc8d9868695a771186e7f1d981 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 26 Nov 2026 10:07:03 +0100 Subject: [PATCH 7/7] test(flags): cover the unsupported_definition precedence with a static cohort dependency --- .../test_feature_flags_person_metadata.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/posthog/test/test_feature_flags_person_metadata.py b/posthog/test/test_feature_flags_person_metadata.py index 6e2973706..11f2040e1 100644 --- a/posthog/test/test_feature_flags_person_metadata.py +++ b/posthog/test/test_feature_flags_person_metadata.py @@ -358,6 +358,36 @@ def test_the_highest_precedence_reason_is_reported(self, _, flags, cohorts): {"target": UnresolvedFlagReason.UNSUPPORTED_DEFINITION}, ) + @parameterized.expand( + [ + ( + "earlier_condition", + [_condition([UNSUPPORTED_REGEX]), _condition([DEPENDS_ON_FIRST_SEEN])], + ), + ( + "same_condition", + [_condition([DEPENDS_ON_FIRST_SEEN, UNSUPPORTED_REGEX])], + ), + ] + ) + def test_static_cohort_dependency_keeps_the_higher_reason(self, _, conditions): + # The dependency needs the server, which ends evaluation. The earlier + # unsupported filter's reason still outranks unresolved_dependency. + dependency = _flag(_condition([IN_STATIC_COHORT]), key="dependency") + client = self._client([_flag(*conditions, key="target"), dependency], {}) + + snapshot = client.evaluate_flags( + "user-123", + person_properties={"name": "foo\n"}, + flag_keys=["target"], + only_evaluate_locally=True, + ) + + self.assertEqual( + dict(snapshot.unresolved_flags), + {"target": UnresolvedFlagReason.UNSUPPORTED_DEFINITION}, + ) + def test_a_cohort_that_stops_early_keeps_the_higher_reason(self): # A dependency on a static cohort ends the cohort at once, whatever comes after it. flags_by_key = {