Skip to content

Commit f75d924

Browse files
authored
fix: disable behaviour for feature flags (#99)
1 parent 617bb53 commit f75d924

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.1 - 2023-04-21
2+
3+
1. Restore how feature flags work when the client library is disabled: All requests return `None` and no events are sent when the client is disabled.
4+
2. Add a `feature_flag_definitions()` debug option, which returns currently loaded feature flag definitions. You can use this to more cleverly decide when to request local evaluation of feature flags.
15
## 3.0.0 - 2023-04-14
26

37
Breaking change:

posthog/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ def get_all_flags_and_payloads(
400400
)
401401

402402

403+
def feature_flag_definitions():
404+
"""Returns loaded feature flags, if any. Helpful for debugging what flag information you have loaded."""
405+
return _proxy("feature_flag_definitions")
406+
407+
403408
def page(*args, **kwargs):
404409
"""Send a page call."""
405410
_proxy("page", *args, **kwargs)

posthog/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ def get_feature_flag(
535535
require("distinct_id", distinct_id, ID_TYPES)
536536
require("groups", groups, dict)
537537

538+
if self.disabled:
539+
return None
540+
538541
if self.feature_flags is None and self.personal_api_key:
539542
self.load_feature_flags()
540543
response = None
@@ -608,6 +611,9 @@ def get_feature_flag_payload(
608611
send_feature_flag_events=True,
609612
disable_geoip=None,
610613
):
614+
if self.disabled:
615+
return None
616+
611617
if match_value is None:
612618
match_value = self.get_feature_flag(
613619
key,
@@ -675,6 +681,9 @@ def get_all_flags_and_payloads(
675681
only_evaluate_locally=False,
676682
disable_geoip=None,
677683
):
684+
if self.disabled:
685+
return {"featureFlags": None, "featureFlagPayloads": None}
686+
678687
flags, payloads, fallback_to_decide = self._get_all_flags_and_payloads_locally(
679688
distinct_id, groups=groups, person_properties=person_properties, group_properties=group_properties
680689
)
@@ -730,6 +739,9 @@ def _get_all_flags_and_payloads_locally(self, distinct_id, *, groups={}, person_
730739

731740
return flags, payloads, fallback_to_decide
732741

742+
def feature_flag_definitions(self):
743+
return self.feature_flags
744+
733745

734746
def require(name, field, data_type):
735747
"""Require that the named `field` has the right `data_type`"""

posthog/test/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,33 @@ def test_disabled(self):
537537

538538
self.assertEqual(msg, "disabled")
539539

540+
@mock.patch("posthog.client.decide")
541+
def test_disabled_with_feature_flags(self, patch_decide):
542+
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)
543+
544+
response = client.get_feature_flag("beta-feature", "12345")
545+
self.assertIsNone(response)
546+
patch_decide.assert_not_called()
547+
548+
response = client.feature_enabled("beta-feature", "12345")
549+
self.assertIsNone(response)
550+
patch_decide.assert_not_called()
551+
552+
response = client.get_all_flags("12345")
553+
self.assertIsNone(response)
554+
patch_decide.assert_not_called()
555+
556+
response = client.get_feature_flag_payload("key", "12345")
557+
self.assertIsNone(response)
558+
patch_decide.assert_not_called()
559+
560+
response = client.get_all_flags_and_payloads("12345")
561+
self.assertEqual(response, {"featureFlags": None, "featureFlagPayloads": None})
562+
patch_decide.assert_not_called()
563+
564+
# no capture calls
565+
self.assertTrue(client.queue.empty())
566+
540567
def test_enabled_to_disabled(self):
541568
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=False)
542569
success, msg = client.capture("distinct_id", "python test event")

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.0.0"
1+
VERSION = "3.0.1"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)