Skip to content

Commit 754c45f

Browse files
authored
fix: propagate missing params in module-level wrapper functions (#479)
* fix: propagate missing params in module-level wrappers Propagate distinct_id for group_identify and flag_keys_to_evaluate for get_all_flags/get_all_flags_and_payloads through the module-level wrapper functions in posthog/__init__.py. These parameters were accepted by Client methods but not forwarded by the convenience wrappers, making them inaccessible when using the posthog.group_identify() etc. module-level API. * docs: add flag_keys_to_evaluate to get_all_flags docstring * test: parameterize flag_keys_to_evaluate tests in test_module * chore: add changeset
1 parent 7223c52 commit 754c45f

3 files changed

Lines changed: 76 additions & 0 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+
fix: propagate missing params in module-level wrapper functions (`distinct_id` for `group_identify`, `flag_keys_to_evaluate` for `get_all_flags`/`get_all_flags_and_payloads`)

posthog/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def group_identify(
391391
timestamp=None, # type: Optional[datetime.datetime]
392392
uuid=None, # type: Optional[str]
393393
disable_geoip=None, # type: Optional[bool]
394+
distinct_id=None, # type: Optional[str]
394395
):
395396
# type: (...) -> Optional[str]
396397
"""
@@ -403,6 +404,7 @@ def group_identify(
403404
timestamp: Optional timestamp for the event
404405
uuid: Optional UUID for the event
405406
disable_geoip: Whether to disable GeoIP lookup
407+
distinct_id: Optional distinct ID of the user performing the action
406408
407409
Examples:
408410
```python
@@ -425,6 +427,7 @@ def group_identify(
425427
timestamp=timestamp,
426428
uuid=uuid,
427429
disable_geoip=disable_geoip,
430+
distinct_id=distinct_id,
428431
)
429432

430433

@@ -611,6 +614,7 @@ def get_all_flags(
611614
only_evaluate_locally=False, # type: bool
612615
disable_geoip=None, # type: Optional[bool]
613616
device_id=None, # type: Optional[str]
617+
flag_keys_to_evaluate=None, # type: Optional[list[str]]
614618
) -> Optional[dict[str, FeatureFlag]]:
615619
"""
616620
Get all flags for a given user.
@@ -622,6 +626,7 @@ def get_all_flags(
622626
group_properties: Group properties
623627
only_evaluate_locally: Whether to evaluate only locally
624628
disable_geoip: Whether to disable GeoIP lookup
629+
flag_keys_to_evaluate: Optional list of flag keys to evaluate (evaluates all if None)
625630
626631
Details:
627632
Flags are key-value pairs where the key is the flag key and the value is the flag variant, or True, or False.
@@ -644,6 +649,7 @@ def get_all_flags(
644649
only_evaluate_locally=only_evaluate_locally,
645650
disable_geoip=disable_geoip,
646651
device_id=device_id,
652+
flag_keys_to_evaluate=flag_keys_to_evaluate,
647653
)
648654

649655

@@ -747,6 +753,7 @@ def get_all_flags_and_payloads(
747753
only_evaluate_locally=False,
748754
disable_geoip=None, # type: Optional[bool]
749755
device_id=None, # type: Optional[str]
756+
flag_keys_to_evaluate=None, # type: Optional[list[str]]
750757
) -> FlagsAndPayloads:
751758
return _proxy(
752759
"get_all_flags_and_payloads",
@@ -757,6 +764,7 @@ def get_all_flags_and_payloads(
757764
only_evaluate_locally=only_evaluate_locally,
758765
disable_geoip=disable_geoip,
759766
device_id=device_id,
767+
flag_keys_to_evaluate=flag_keys_to_evaluate,
760768
)
761769

762770

posthog/test/test_module.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import unittest
2+
from unittest import mock
23

4+
from parameterized import parameterized
5+
6+
import posthog
37
from posthog import Posthog
48

59

@@ -30,3 +34,62 @@ def test_alias(self):
3034

3135
def test_flush(self):
3236
self.posthog.flush()
37+
38+
39+
class TestModuleLevelWrappers(unittest.TestCase):
40+
"""Test that module-level wrapper functions in posthog/__init__.py
41+
correctly propagate all parameters to the Client methods."""
42+
43+
def setUp(self):
44+
self.mock_client = mock.MagicMock()
45+
self._original_client = posthog.default_client
46+
posthog.default_client = self.mock_client
47+
48+
def tearDown(self):
49+
posthog.default_client = self._original_client
50+
51+
def test_group_identify_propagates_distinct_id(self):
52+
posthog.group_identify(
53+
"company",
54+
"company_123",
55+
{"name": "Awesome Inc."},
56+
distinct_id="user_456",
57+
)
58+
self.mock_client.group_identify.assert_called_once_with(
59+
group_type="company",
60+
group_key="company_123",
61+
properties={"name": "Awesome Inc."},
62+
timestamp=None,
63+
uuid=None,
64+
disable_geoip=None,
65+
distinct_id="user_456",
66+
)
67+
68+
def test_group_identify_distinct_id_defaults_to_none(self):
69+
posthog.group_identify("company", "company_123")
70+
call_kwargs = self.mock_client.group_identify.call_args[1]
71+
self.assertIsNone(call_kwargs["distinct_id"])
72+
73+
@parameterized.expand(
74+
[
75+
("get_all_flags", "get_all_flags"),
76+
("get_all_flags_and_payloads", "get_all_flags_and_payloads"),
77+
]
78+
)
79+
def test_flag_keys_to_evaluate_propagated(self, _name, method_name):
80+
fn = getattr(posthog, method_name)
81+
fn("user_123", flag_keys_to_evaluate=["flag-1", "flag-2"])
82+
call_kwargs = getattr(self.mock_client, method_name).call_args[1]
83+
self.assertEqual(call_kwargs["flag_keys_to_evaluate"], ["flag-1", "flag-2"])
84+
85+
@parameterized.expand(
86+
[
87+
("get_all_flags", "get_all_flags"),
88+
("get_all_flags_and_payloads", "get_all_flags_and_payloads"),
89+
]
90+
)
91+
def test_flag_keys_to_evaluate_defaults_to_none(self, _name, method_name):
92+
fn = getattr(posthog, method_name)
93+
fn("user_123")
94+
call_kwargs = getattr(self.mock_client, method_name).call_args[1]
95+
self.assertIsNone(call_kwargs["flag_keys_to_evaluate"])

0 commit comments

Comments
 (0)