Skip to content

Commit 5f97812

Browse files
committed
fix: Ignore new flag filter type in local evaluation
1 parent c4e09cd commit 5f97812

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

posthog/feature_flags.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ def is_condition_match(
109109
property_type = prop.get("type")
110110
if property_type == "cohort":
111111
matches = match_cohort(prop, properties, cohort_properties)
112+
elif property_type == "flag":
113+
log.warning(
114+
"Flag dependency filters are not supported in local evaluation. "
115+
"Skipping condition for flag '%s' with dependency on flag '%s'",
116+
feature_flag.get("key", "unknown"),
117+
prop.get("key", "unknown"),
118+
)
119+
continue
112120
else:
113121
matches = match_property(prop, properties)
114122
if not matches:
@@ -317,6 +325,13 @@ def match_property_group(property_group, property_values, cohort_properties) ->
317325
try:
318326
if prop.get("type") == "cohort":
319327
matches = match_cohort(prop, property_values, cohort_properties)
328+
elif prop.get("type") == "flag":
329+
log.warning(
330+
"Flag dependency filters are not supported in local evaluation. "
331+
"Skipping condition with dependency on flag '%s'",
332+
prop.get("key", "unknown"),
333+
)
334+
continue
320335
else:
321336
matches = match_property(prop, property_values)
322337

posthog/test/test_feature_flags.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,77 @@ def test_feature_flags_local_evaluation_for_negated_cohorts(
13551355
self.assertEqual(patch_flags.call_count, 0)
13561356
self.assertEqual(patch_get.call_count, 0)
13571357

1358+
@mock.patch("posthog.feature_flags.log")
1359+
@mock.patch("posthog.client.flags")
1360+
@mock.patch("posthog.client.get")
1361+
def test_feature_flags_with_flag_dependencies(
1362+
self, patch_get, patch_flags, mock_log
1363+
):
1364+
client = Client(FAKE_TEST_API_KEY, personal_api_key=FAKE_TEST_API_KEY)
1365+
client.feature_flags = [
1366+
{
1367+
"id": 1,
1368+
"name": "Flag with Dependencies",
1369+
"key": "flag-with-dependencies",
1370+
"active": True,
1371+
"filters": {
1372+
"groups": [
1373+
{
1374+
"properties": [
1375+
{
1376+
"key": "beta-feature",
1377+
"operator": "exact",
1378+
"value": True,
1379+
"type": "flag",
1380+
},
1381+
{
1382+
"key": "email",
1383+
"operator": "icontains",
1384+
"value": "@example.com",
1385+
"type": "person",
1386+
},
1387+
],
1388+
"rollout_percentage": 100,
1389+
}
1390+
],
1391+
},
1392+
}
1393+
]
1394+
1395+
# Test that flag evaluation doesn't fail when encountering a flag dependency
1396+
# The flag should evaluate based on other conditions (email contains @example.com)
1397+
# Since flag dependencies aren't implemented, it should skip the flag condition
1398+
# and evaluate based on the email condition only
1399+
feature_flag_match = client.get_feature_flag(
1400+
"flag-with-dependencies",
1401+
"test-user",
1402+
person_properties={"email": "[email protected]"},
1403+
)
1404+
self.assertEqual(feature_flag_match, True)
1405+
self.assertEqual(patch_flags.call_count, 0)
1406+
self.assertEqual(patch_get.call_count, 0)
1407+
1408+
# Verify warning was logged for flag dependency
1409+
mock_log.warning.assert_called_with(
1410+
"Flag dependency filters are not supported in local evaluation. "
1411+
"Skipping condition for flag '%s' with dependency on flag '%s'",
1412+
"flag-with-dependencies",
1413+
"beta-feature",
1414+
)
1415+
1416+
# Test with email that doesn't match
1417+
feature_flag_match = client.get_feature_flag(
1418+
"flag-with-dependencies",
1419+
"test-user-2",
1420+
person_properties={"email": "[email protected]"},
1421+
)
1422+
self.assertEqual(feature_flag_match, False)
1423+
self.assertEqual(patch_flags.call_count, 0)
1424+
self.assertEqual(patch_get.call_count, 0)
1425+
1426+
# Verify warning was logged again for the second evaluation
1427+
self.assertEqual(mock_log.warning.call_count, 2)
1428+
13581429
@mock.patch("posthog.client.Poller")
13591430
@mock.patch("posthog.client.get")
13601431
def test_load_feature_flags(self, patch_get, patch_poll):

0 commit comments

Comments
 (0)