Skip to content

Commit b4103b3

Browse files
authored
fix: add function for active variants (#89)
* add function for active variants * format
1 parent 1aeffa9 commit b4103b3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

posthog/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ def get_feature_variants(self, distinct_id, groups=None, person_properties=None,
130130
resp_data = self.get_decide(distinct_id, groups, person_properties, group_properties)
131131
return resp_data["featureFlags"]
132132

133+
def _get_active_feature_variants(self, distinct_id, groups=None, person_properties=None, group_properties=None):
134+
feature_variants = self.get_feature_variants(distinct_id, groups, person_properties, group_properties)
135+
return {
136+
k: v for (k, v) in feature_variants.items() if v is not False
137+
} # explicitly test for false to account for values that may seem falsy (ex: 0)
138+
133139
def get_feature_payloads(self, distinct_id, groups=None, person_properties=None, group_properties=None):
134140
resp_data = self.get_decide(distinct_id, groups, person_properties, group_properties)
135141
return resp_data["featureFlagPayloads"]
@@ -183,7 +189,7 @@ def capture(
183189

184190
if send_feature_flags:
185191
try:
186-
feature_variants = self.get_feature_variants(distinct_id, groups)
192+
feature_variants = self._get_active_feature_variants(distinct_id, groups)
187193
except Exception as e:
188194
self.log.exception(f"[FEATURE FLAGS] Unable to get feature variants: {e}")
189195
else:

posthog/test/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ def test_basic_capture_with_feature_flags(self, patch_decide):
106106

107107
self.assertEqual(patch_decide.call_count, 1)
108108

109+
@mock.patch("posthog.client.decide")
110+
def test_get_active_feature_flags(self, patch_decide):
111+
patch_decide.return_value = {
112+
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
113+
}
114+
115+
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)
116+
variants = client._get_active_feature_variants("some_id", None, None, None)
117+
print(variants)
118+
119+
@mock.patch("posthog.client.decide")
120+
def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide):
121+
patch_decide.return_value = {
122+
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
123+
}
124+
125+
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)
126+
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=True)
127+
client.flush()
128+
self.assertTrue(success)
129+
self.assertFalse(self.failed)
130+
131+
self.assertEqual(msg["event"], "python test event")
132+
self.assertTrue(isinstance(msg["timestamp"], str))
133+
self.assertIsNone(msg.get("uuid"))
134+
self.assertEqual(msg["distinct_id"], "distinct_id")
135+
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
136+
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
137+
self.assertEqual(msg["properties"]["$feature/beta-feature"], "random-variant")
138+
self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
139+
self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature", "alpha-feature"])
140+
141+
self.assertEqual(patch_decide.call_count, 1)
142+
109143
@mock.patch("posthog.client.decide")
110144
def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(self, patch_decide):
111145
patch_decide.return_value = {"featureFlags": {"beta-feature": "random-variant"}}

0 commit comments

Comments
 (0)