Skip to content

Commit 034dce2

Browse files
authored
fix: no-op global client for blank api key (#632)
1 parent b0726d1 commit 034dce2

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
Make module-level setup no-op when API key is blank

posthog/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ def get_tags() -> Dict[str, Any]:
266266
the corresponding constructor arguments.
267267
268268
Attributes:
269-
api_key: Project API key/token used by the global client. Required before
270-
calling any global capture or feature flag API.
269+
api_key: Project API key/token used by the global client. Missing or blank
270+
values create a disabled no-op global client.
271271
host: PostHog ingestion host. Defaults to the US ingestion endpoint when not
272272
set.
273273
on_error: Optional callback invoked by background consumers when event upload
@@ -1064,20 +1064,17 @@ def setup() -> Client:
10641064
``setup()`` is called automatically by global APIs such as ``capture()``.
10651065
10661066
Returns:
1067-
The global ``Client`` instance.
1068-
1069-
Raises:
1070-
ValueError: If ``api_key`` has not been configured.
1067+
The global ``Client`` instance. If ``api_key`` is missing or blank,
1068+
the client is disabled and module-level calls become no-ops.
10711069
10721070
Category:
10731071
Initialization
10741072
"""
10751073
global default_client
10761074
if not default_client:
1077-
if not api_key:
1078-
raise ValueError("API key is required")
1075+
configured_api_key = api_key.strip() if api_key else ""
10791076
default_client = Client(
1080-
api_key,
1077+
configured_api_key,
10811078
host=host,
10821079
debug=debug,
10831080
on_error=on_error,

posthog/test/test_module.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def setUp(self):
4343
self._original_disabled = posthog.disabled
4444
self._original_send = posthog.send
4545
posthog.default_client = None
46-
posthog.api_key = " \n\t "
4746
posthog.disabled = False
4847
posthog.send = False
4948

@@ -53,12 +52,24 @@ def tearDown(self):
5352
posthog.disabled = self._original_disabled
5453
posthog.send = self._original_send
5554

56-
def test_setup_preserves_client_disabled_when_trimmed_api_key_is_empty(self):
57-
posthog.setup()
55+
@parameterized.expand(
56+
[
57+
("unset", None),
58+
("empty", ""),
59+
("whitespace", " \n\t "),
60+
]
61+
)
62+
def test_setup_configures_disabled_client_when_api_key_is_blank(
63+
self, _name, api_key
64+
):
65+
posthog.api_key = api_key
66+
67+
client = posthog.setup()
5868

59-
self.assertIsNotNone(posthog.default_client)
60-
self.assertEqual(posthog.default_client.api_key, "")
61-
self.assertTrue(posthog.default_client.disabled)
69+
self.assertIs(client, posthog.default_client)
70+
self.assertEqual(client.api_key, "")
71+
self.assertTrue(client.disabled)
72+
self.assertIsNone(posthog.capture("Module Python Event", distinct_id="john"))
6273

6374

6475
class TestModuleLevelWrappers(unittest.TestCase):

0 commit comments

Comments
 (0)