Skip to content

Commit 2e99081

Browse files
authored
Fix feature flag issue with no % rollout (#30)
* fix feature flag issue with no % rollout * black
1 parent 372fb74 commit 2e99081

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

posthog/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ def feature_enabled(self, key, distinct_id, default=False):
330330
except IndexError:
331331
return default
332332

333-
if feature_flag.get("is_simple_flag") and feature_flag.get("rollout_percentage"):
334-
response = _hash(key, distinct_id) <= (feature_flag["rollout_percentage"] / 100)
333+
if feature_flag.get("is_simple_flag"):
334+
response = _hash(key, distinct_id) <= ((feature_flag.get("rollout_percentage", 100) or 100) / 100)
335335
else:
336336
try:
337337
request_data = {

posthog/test/test_client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,28 @@ def test_feature_enabled_simple(self, patch_get):
325325
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
326326

327327
@mock.patch("posthog.client.decide")
328-
def test_feature_enabled_request(self, patch_get):
329-
patch_get.return_value = {"featureFlags": ["beta-feature"]}
328+
def test_feature_enabled_request(self, patch_decide):
329+
patch_decide.return_value = {"featureFlags": ["beta-feature"]}
330330
client = Client(TEST_API_KEY)
331331
client.feature_flags = [
332332
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
333333
]
334334
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
335335

336+
@mock.patch("posthog.client.get")
337+
def test_feature_enabled_simple_without_rollout_percentage(self, patch_get):
338+
client = Client(TEST_API_KEY)
339+
client.feature_flags = [{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True}]
340+
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
341+
342+
@mock.patch("posthog.client.get")
343+
def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get):
344+
client = Client(TEST_API_KEY)
345+
client.feature_flags = [
346+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percantage": None}
347+
]
348+
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
349+
336350
@mock.patch("posthog.client.Poller")
337351
@mock.patch("posthog.client.get")
338352
def test_feature_enabled_doesnt_exist(self, patch_get, patch_poll):

0 commit comments

Comments
 (0)