Skip to content

Commit

Permalink
fix: disable behaviour for feature flags (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Apr 21, 2023
1 parent 617bb53 commit f75d924
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.1 - 2023-04-21

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.
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.
## 3.0.0 - 2023-04-14

Breaking change:
Expand Down
5 changes: 5 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ def get_all_flags_and_payloads(
)


def feature_flag_definitions():
"""Returns loaded feature flags, if any. Helpful for debugging what flag information you have loaded."""
return _proxy("feature_flag_definitions")


def page(*args, **kwargs):
"""Send a page call."""
_proxy("page", *args, **kwargs)
Expand Down
12 changes: 12 additions & 0 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ def get_feature_flag(
require("distinct_id", distinct_id, ID_TYPES)
require("groups", groups, dict)

if self.disabled:
return None

if self.feature_flags is None and self.personal_api_key:
self.load_feature_flags()
response = None
Expand Down Expand Up @@ -608,6 +611,9 @@ def get_feature_flag_payload(
send_feature_flag_events=True,
disable_geoip=None,
):
if self.disabled:
return None

if match_value is None:
match_value = self.get_feature_flag(
key,
Expand Down Expand Up @@ -675,6 +681,9 @@ def get_all_flags_and_payloads(
only_evaluate_locally=False,
disable_geoip=None,
):
if self.disabled:
return {"featureFlags": None, "featureFlagPayloads": None}

flags, payloads, fallback_to_decide = self._get_all_flags_and_payloads_locally(
distinct_id, groups=groups, person_properties=person_properties, group_properties=group_properties
)
Expand Down Expand Up @@ -730,6 +739,9 @@ def _get_all_flags_and_payloads_locally(self, distinct_id, *, groups={}, person_

return flags, payloads, fallback_to_decide

def feature_flag_definitions(self):
return self.feature_flags


def require(name, field, data_type):
"""Require that the named `field` has the right `data_type`"""
Expand Down
27 changes: 27 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,33 @@ def test_disabled(self):

self.assertEqual(msg, "disabled")

@mock.patch("posthog.client.decide")
def test_disabled_with_feature_flags(self, patch_decide):
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)

response = client.get_feature_flag("beta-feature", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()

response = client.feature_enabled("beta-feature", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()

response = client.get_all_flags("12345")
self.assertIsNone(response)
patch_decide.assert_not_called()

response = client.get_feature_flag_payload("key", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()

response = client.get_all_flags_and_payloads("12345")
self.assertEqual(response, {"featureFlags": None, "featureFlagPayloads": None})
patch_decide.assert_not_called()

# no capture calls
self.assertTrue(client.queue.empty())

def test_enabled_to_disabled(self):
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=False)
success, msg = client.capture("distinct_id", "python test event")
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "3.0.0"
VERSION = "3.0.1"

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

0 comments on commit f75d924

Please sign in to comment.