Skip to content

Commit 4155080

Browse files
authoredFeb 10, 2025
Deprecate the context argument (#182)
1 parent 994003f commit 4155080

File tree

4 files changed

+106
-44
lines changed

4 files changed

+106
-44
lines changed
 

‎posthog/__init__.py

+41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime # noqa: F401
2+
import warnings
23
from typing import Callable, Dict, List, Optional, Tuple # noqa: F401
34

45
from posthog.client import Client
@@ -43,6 +44,13 @@ def capture(
4344
send_feature_flags=False,
4445
disable_geoip=None, # type: Optional[bool]
4546
):
47+
if context is not None:
48+
warnings.warn(
49+
"The 'context' parameter is deprecated and will be removed in a future version.",
50+
DeprecationWarning,
51+
stacklevel=2,
52+
)
53+
4654
# type: (...) -> Tuple[bool, dict]
4755
"""
4856
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.
@@ -86,6 +94,13 @@ def identify(
8694
uuid=None, # type: Optional[str]
8795
disable_geoip=None, # type: Optional[bool]
8896
):
97+
if context is not None:
98+
warnings.warn(
99+
"The 'context' parameter is deprecated and will be removed in a future version.",
100+
DeprecationWarning,
101+
stacklevel=2,
102+
)
103+
89104
# type: (...) -> Tuple[bool, dict]
90105
"""
91106
Identify lets you add metadata on your users so you can more easily identify who they are in PostHog, and even do things like segment users by these properties.
@@ -121,6 +136,13 @@ def set(
121136
uuid=None, # type: Optional[str]
122137
disable_geoip=None, # type: Optional[bool]
123138
):
139+
if context is not None:
140+
warnings.warn(
141+
"The 'context' parameter is deprecated and will be removed in a future version.",
142+
DeprecationWarning,
143+
stacklevel=2,
144+
)
145+
124146
# type: (...) -> Tuple[bool, dict]
125147
"""
126148
Set properties on a user record.
@@ -156,6 +178,13 @@ def set_once(
156178
uuid=None, # type: Optional[str]
157179
disable_geoip=None, # type: Optional[bool]
158180
):
181+
if context is not None:
182+
warnings.warn(
183+
"The 'context' parameter is deprecated and will be removed in a future version.",
184+
DeprecationWarning,
185+
stacklevel=2,
186+
)
187+
159188
# type: (...) -> Tuple[bool, dict]
160189
"""
161190
Set properties on a user record, only if they do not yet exist.
@@ -192,6 +221,12 @@ def group_identify(
192221
uuid=None, # type: Optional[str]
193222
disable_geoip=None, # type: Optional[bool]
194223
):
224+
if context is not None:
225+
warnings.warn(
226+
"The 'context' parameter is deprecated and will be removed in a future version.",
227+
DeprecationWarning,
228+
stacklevel=2,
229+
)
195230
# type: (...) -> Tuple[bool, dict]
196231
"""
197232
Set properties on a group
@@ -228,6 +263,12 @@ def alias(
228263
uuid=None, # type: Optional[str]
229264
disable_geoip=None, # type: Optional[bool]
230265
):
266+
if context is not None:
267+
warnings.warn(
268+
"The 'context' parameter is deprecated and will be removed in a future version.",
269+
DeprecationWarning,
270+
stacklevel=2,
271+
)
231272
# type: (...) -> Tuple[bool, dict]
232273
"""
233274
To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?"

‎posthog/client.py

+55-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numbers
44
import os
55
import sys
6+
import warnings
67
from datetime import datetime, timedelta
78
from uuid import UUID, uuid4
89

@@ -147,14 +148,19 @@ def __init__(
147148
consumer.start()
148149

149150
def identify(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
151+
if context is not None:
152+
warnings.warn(
153+
"The 'context' parameter is deprecated and will be removed in a future version.",
154+
DeprecationWarning,
155+
stacklevel=2,
156+
)
157+
150158
properties = properties or {}
151-
context = context or {}
152159
require("distinct_id", distinct_id, ID_TYPES)
153160
require("properties", properties, dict)
154161

155162
msg = {
156163
"timestamp": timestamp,
157-
"context": context,
158164
"distinct_id": distinct_id,
159165
"$set": properties,
160166
"event": "$identify",
@@ -218,16 +224,21 @@ def capture(
218224
send_feature_flags=False,
219225
disable_geoip=None,
220226
):
227+
if context is not None:
228+
warnings.warn(
229+
"The 'context' parameter is deprecated and will be removed in a future version.",
230+
DeprecationWarning,
231+
stacklevel=2,
232+
)
233+
221234
properties = properties or {}
222-
context = context or {}
223235
require("distinct_id", distinct_id, ID_TYPES)
224236
require("properties", properties, dict)
225237
require("event", event, string_types)
226238

227239
msg = {
228240
"properties": properties,
229241
"timestamp": timestamp,
230-
"context": context,
231242
"distinct_id": distinct_id,
232243
"event": event,
233244
"uuid": uuid,
@@ -264,14 +275,19 @@ def capture(
264275
return self._enqueue(msg, disable_geoip)
265276

266277
def set(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
278+
if context is not None:
279+
warnings.warn(
280+
"The 'context' parameter is deprecated and will be removed in a future version.",
281+
DeprecationWarning,
282+
stacklevel=2,
283+
)
284+
267285
properties = properties or {}
268-
context = context or {}
269286
require("distinct_id", distinct_id, ID_TYPES)
270287
require("properties", properties, dict)
271288

272289
msg = {
273290
"timestamp": timestamp,
274-
"context": context,
275291
"distinct_id": distinct_id,
276292
"$set": properties,
277293
"event": "$set",
@@ -281,14 +297,19 @@ def set(self, distinct_id=None, properties=None, context=None, timestamp=None, u
281297
return self._enqueue(msg, disable_geoip)
282298

283299
def set_once(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
300+
if context is not None:
301+
warnings.warn(
302+
"The 'context' parameter is deprecated and will be removed in a future version.",
303+
DeprecationWarning,
304+
stacklevel=2,
305+
)
306+
284307
properties = properties or {}
285-
context = context or {}
286308
require("distinct_id", distinct_id, ID_TYPES)
287309
require("properties", properties, dict)
288310

289311
msg = {
290312
"timestamp": timestamp,
291-
"context": context,
292313
"distinct_id": distinct_id,
293314
"$set_once": properties,
294315
"event": "$set_once",
@@ -308,8 +329,13 @@ def group_identify(
308329
disable_geoip=None,
309330
distinct_id=None,
310331
):
332+
if context is not None:
333+
warnings.warn(
334+
"The 'context' parameter is deprecated and will be removed in a future version.",
335+
DeprecationWarning,
336+
stacklevel=2,
337+
)
311338
properties = properties or {}
312-
context = context or {}
313339
require("group_type", group_type, ID_TYPES)
314340
require("group_key", group_key, ID_TYPES)
315341
require("properties", properties, dict)
@@ -328,14 +354,18 @@ def group_identify(
328354
},
329355
"distinct_id": distinct_id,
330356
"timestamp": timestamp,
331-
"context": context,
332357
"uuid": uuid,
333358
}
334359

335360
return self._enqueue(msg, disable_geoip)
336361

337362
def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
338-
context = context or {}
363+
if context is not None:
364+
warnings.warn(
365+
"The 'context' parameter is deprecated and will be removed in a future version.",
366+
DeprecationWarning,
367+
stacklevel=2,
368+
)
339369

340370
require("previous_id", previous_id, ID_TYPES)
341371
require("distinct_id", distinct_id, ID_TYPES)
@@ -346,7 +376,6 @@ def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None
346376
"alias": distinct_id,
347377
},
348378
"timestamp": timestamp,
349-
"context": context,
350379
"event": "$create_alias",
351380
"distinct_id": previous_id,
352381
}
@@ -356,9 +385,14 @@ def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None
356385
def page(
357386
self, distinct_id=None, url=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None
358387
):
359-
properties = properties or {}
360-
context = context or {}
388+
if context is not None:
389+
warnings.warn(
390+
"The 'context' parameter is deprecated and will be removed in a future version.",
391+
DeprecationWarning,
392+
stacklevel=2,
393+
)
361394

395+
properties = properties or {}
362396
require("distinct_id", distinct_id, ID_TYPES)
363397
require("properties", properties, dict)
364398

@@ -369,7 +403,6 @@ def page(
369403
"event": "$pageview",
370404
"properties": properties,
371405
"timestamp": timestamp,
372-
"context": context,
373406
"distinct_id": distinct_id,
374407
"uuid": uuid,
375408
}
@@ -386,6 +419,13 @@ def capture_exception(
386419
uuid=None,
387420
groups=None,
388421
):
422+
if context is not None:
423+
warnings.warn(
424+
"The 'context' parameter is deprecated and will be removed in a future version.",
425+
DeprecationWarning,
426+
stacklevel=2,
427+
)
428+
389429
# this function shouldn't ever throw an error, so it logs exceptions instead of raising them.
390430
# this is important to ensure we don't unexpectedly re-raise exceptions in the user's code.
391431
try:
@@ -446,7 +486,6 @@ def _enqueue(self, msg, disable_geoip):
446486
timestamp = datetime.now(tz=tzutc())
447487

448488
require("timestamp", timestamp, datetime)
449-
require("context", msg["context"], dict)
450489

451490
# add common
452491
timestamp = guess_timezone(timestamp)

‎posthog/test/test_client.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -581,16 +581,14 @@ def test_advanced_capture(self):
581581
"distinct_id",
582582
"python test event",
583583
{"property": "value"},
584-
{"ip": "192.168.0.1"},
585-
datetime(2014, 9, 3),
586-
"new-uuid",
584+
timestamp=datetime(2014, 9, 3),
585+
uuid="new-uuid",
587586
)
588587

589588
self.assertTrue(success)
590589

591590
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
592591
self.assertEqual(msg["properties"]["property"], "value")
593-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
594592
self.assertEqual(msg["event"], "python test event")
595593
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
596594
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
@@ -623,13 +621,12 @@ def test_basic_identify(self):
623621
def test_advanced_identify(self):
624622
client = self.client
625623
success, msg = client.identify(
626-
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
624+
"distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid"
627625
)
628626

629627
self.assertTrue(success)
630628

631629
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
632-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
633630
self.assertEqual(msg["$set"]["trait"], "value")
634631
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
635632
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
@@ -651,14 +648,11 @@ def test_basic_set(self):
651648

652649
def test_advanced_set(self):
653650
client = self.client
654-
success, msg = client.set(
655-
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
656-
)
651+
success, msg = client.set("distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid")
657652

658653
self.assertTrue(success)
659654

660655
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
661-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
662656
self.assertEqual(msg["$set"]["trait"], "value")
663657
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
664658
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
@@ -681,13 +675,12 @@ def test_basic_set_once(self):
681675
def test_advanced_set_once(self):
682676
client = self.client
683677
success, msg = client.set_once(
684-
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
678+
"distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid"
685679
)
686680

687681
self.assertTrue(success)
688682

689683
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
690-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
691684
self.assertEqual(msg["$set_once"]["trait"], "value")
692685
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
693686
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
@@ -736,7 +729,7 @@ def test_basic_group_identify_with_distinct_id(self):
736729

737730
def test_advanced_group_identify(self):
738731
success, msg = self.client.group_identify(
739-
"organization", "id:5", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
732+
"organization", "id:5", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid"
740733
)
741734

742735
self.assertTrue(success)
@@ -754,16 +747,14 @@ def test_advanced_group_identify(self):
754747
},
755748
)
756749
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
757-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
758750

759751
def test_advanced_group_identify_with_distinct_id(self):
760752
success, msg = self.client.group_identify(
761753
"organization",
762754
"id:5",
763755
{"trait": "value"},
764-
{"ip": "192.168.0.1"},
765-
datetime(2014, 9, 3),
766-
"new-uuid",
756+
timestamp=datetime(2014, 9, 3),
757+
uuid="new-uuid",
767758
distinct_id="distinct_id",
768759
)
769760

@@ -783,7 +774,6 @@ def test_advanced_group_identify_with_distinct_id(self):
783774
},
784775
)
785776
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
786-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
787777

788778
def test_basic_alias(self):
789779
client = self.client
@@ -819,15 +809,13 @@ def test_advanced_page(self):
819809
"distinct_id",
820810
"https://posthog.com/contact",
821811
{"property": "value"},
822-
{"ip": "192.168.0.1"},
823-
datetime(2014, 9, 3),
824-
"new-uuid",
812+
timestamp=datetime(2014, 9, 3),
813+
uuid="new-uuid",
825814
)
826815

827816
self.assertTrue(success)
828817

829818
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
830-
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
831819
self.assertEqual(msg["properties"]["$current_url"], "https://posthog.com/contact")
832820
self.assertEqual(msg["properties"]["property"], "value")
833821
self.assertEqual(msg["properties"]["$lib"], "posthog-python")

‎simulator.py

-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def json_hash(str):
2424

2525
parser.add_argument("--distinct_id", help="the user id to send the event as")
2626
parser.add_argument("--anonymousId", help="the anonymous user id to send the event as")
27-
parser.add_argument("--context", help="additional context for the event (JSON-encoded)")
2827

2928
parser.add_argument("--event", help="the event name to send with the event")
3029
parser.add_argument("--properties", help="the event properties to send (JSON-encoded)")
@@ -48,7 +47,6 @@ def capture():
4847
options.event,
4948
anonymous_id=options.anonymousId,
5049
properties=json_hash(options.properties),
51-
context=json_hash(options.context),
5250
)
5351

5452

@@ -58,7 +56,6 @@ def page():
5856
name=options.name,
5957
anonymous_id=options.anonymousId,
6058
properties=json_hash(options.properties),
61-
context=json_hash(options.context),
6259
)
6360

6461

@@ -67,23 +64,20 @@ def identify():
6764
options.distinct_id,
6865
anonymous_id=options.anonymousId,
6966
traits=json_hash(options.traits),
70-
context=json_hash(options.context),
7167
)
7268

7369

7470
def set_once():
7571
posthog.set_once(
7672
options.distinct_id,
7773
properties=json_hash(options.traits),
78-
context=json_hash(options.context),
7974
)
8075

8176

8277
def set():
8378
posthog.set(
8479
options.distinct_id,
8580
properties=json_hash(options.traits),
86-
context=json_hash(options.context),
8781
)
8882

8983

0 commit comments

Comments
 (0)
Please sign in to comment.