Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the context argument #182

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime # noqa: F401
import warnings
from typing import Callable, Dict, List, Optional, Tuple # noqa: F401

from posthog.client import Client
Expand Down Expand Up @@ -43,6 +44,13 @@ def capture(
send_feature_flags=False,
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

# type: (...) -> Tuple[bool, dict]
"""
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.
Expand Down Expand Up @@ -86,6 +94,13 @@ def identify(
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

# type: (...) -> Tuple[bool, dict]
"""
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.
Expand Down Expand Up @@ -121,6 +136,13 @@ def set(
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

# type: (...) -> Tuple[bool, dict]
"""
Set properties on a user record.
Expand Down Expand Up @@ -156,6 +178,13 @@ def set_once(
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

# type: (...) -> Tuple[bool, dict]
"""
Set properties on a user record, only if they do not yet exist.
Expand Down Expand Up @@ -192,6 +221,12 @@ def group_identify(
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
# type: (...) -> Tuple[bool, dict]
"""
Set properties on a group
Expand Down Expand Up @@ -228,6 +263,12 @@ def alias(
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
# type: (...) -> Tuple[bool, dict]
"""
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?"
Expand Down
71 changes: 55 additions & 16 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numbers
import os
import sys
import warnings
from datetime import datetime, timedelta
from uuid import UUID, uuid4

Expand Down Expand Up @@ -147,14 +148,19 @@ def __init__(
consumer.start()

def identify(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

properties = properties or {}
context = context or {}
require("distinct_id", distinct_id, ID_TYPES)
require("properties", properties, dict)

msg = {
"timestamp": timestamp,
"context": context,
"distinct_id": distinct_id,
"$set": properties,
"event": "$identify",
Expand Down Expand Up @@ -218,16 +224,21 @@ def capture(
send_feature_flags=False,
disable_geoip=None,
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

properties = properties or {}
context = context or {}
require("distinct_id", distinct_id, ID_TYPES)
require("properties", properties, dict)
require("event", event, string_types)

msg = {
"properties": properties,
"timestamp": timestamp,
"context": context,
"distinct_id": distinct_id,
"event": event,
"uuid": uuid,
Expand Down Expand Up @@ -264,14 +275,19 @@ def capture(
return self._enqueue(msg, disable_geoip)

def set(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

properties = properties or {}
context = context or {}
require("distinct_id", distinct_id, ID_TYPES)
require("properties", properties, dict)

msg = {
"timestamp": timestamp,
"context": context,
"distinct_id": distinct_id,
"$set": properties,
"event": "$set",
Expand All @@ -281,14 +297,19 @@ def set(self, distinct_id=None, properties=None, context=None, timestamp=None, u
return self._enqueue(msg, disable_geoip)

def set_once(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

properties = properties or {}
context = context or {}
require("distinct_id", distinct_id, ID_TYPES)
require("properties", properties, dict)

msg = {
"timestamp": timestamp,
"context": context,
"distinct_id": distinct_id,
"$set_once": properties,
"event": "$set_once",
Expand All @@ -308,8 +329,13 @@ def group_identify(
disable_geoip=None,
distinct_id=None,
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
properties = properties or {}
context = context or {}
require("group_type", group_type, ID_TYPES)
require("group_key", group_key, ID_TYPES)
require("properties", properties, dict)
Expand All @@ -328,14 +354,18 @@ def group_identify(
},
"distinct_id": distinct_id,
"timestamp": timestamp,
"context": context,
"uuid": uuid,
}

return self._enqueue(msg, disable_geoip)

def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None, uuid=None, disable_geoip=None):
context = context or {}
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

require("previous_id", previous_id, ID_TYPES)
require("distinct_id", distinct_id, ID_TYPES)
Expand All @@ -346,7 +376,6 @@ def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None
"alias": distinct_id,
},
"timestamp": timestamp,
"context": context,
"event": "$create_alias",
"distinct_id": previous_id,
}
Expand All @@ -356,9 +385,14 @@ def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None
def page(
self, distinct_id=None, url=None, properties=None, context=None, timestamp=None, uuid=None, disable_geoip=None
):
properties = properties or {}
context = context or {}
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

properties = properties or {}
require("distinct_id", distinct_id, ID_TYPES)
require("properties", properties, dict)

Expand All @@ -369,7 +403,6 @@ def page(
"event": "$pageview",
"properties": properties,
"timestamp": timestamp,
"context": context,
"distinct_id": distinct_id,
"uuid": uuid,
}
Expand All @@ -386,6 +419,13 @@ def capture_exception(
uuid=None,
groups=None,
):
if context is not None:
warnings.warn(
"The 'context' parameter is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)

# this function shouldn't ever throw an error, so it logs exceptions instead of raising them.
# this is important to ensure we don't unexpectedly re-raise exceptions in the user's code.
try:
Expand Down Expand Up @@ -446,7 +486,6 @@ def _enqueue(self, msg, disable_geoip):
timestamp = datetime.now(tz=tzutc())

require("timestamp", timestamp, datetime)
require("context", msg["context"], dict)

# add common
timestamp = guess_timezone(timestamp)
Expand Down
32 changes: 10 additions & 22 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,16 +581,14 @@ def test_advanced_capture(self):
"distinct_id",
"python test event",
{"property": "value"},
{"ip": "192.168.0.1"},
datetime(2014, 9, 3),
"new-uuid",
timestamp=datetime(2014, 9, 3),
uuid="new-uuid",
)

self.assertTrue(success)

self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
self.assertEqual(msg["properties"]["property"], "value")
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
self.assertEqual(msg["event"], "python test event")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
Expand Down Expand Up @@ -623,13 +621,12 @@ def test_basic_identify(self):
def test_advanced_identify(self):
client = self.client
success, msg = client.identify(
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
"distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid"
)

self.assertTrue(success)

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

def test_advanced_set(self):
client = self.client
success, msg = client.set(
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
)
success, msg = client.set("distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid")

self.assertTrue(success)

self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
self.assertEqual(msg["$set"]["trait"], "value")
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
Expand All @@ -681,13 +675,12 @@ def test_basic_set_once(self):
def test_advanced_set_once(self):
client = self.client
success, msg = client.set_once(
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
"distinct_id", {"trait": "value"}, timestamp=datetime(2014, 9, 3), uuid="new-uuid"
)

self.assertTrue(success)

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

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

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

def test_advanced_group_identify_with_distinct_id(self):
success, msg = self.client.group_identify(
"organization",
"id:5",
{"trait": "value"},
{"ip": "192.168.0.1"},
datetime(2014, 9, 3),
"new-uuid",
timestamp=datetime(2014, 9, 3),
uuid="new-uuid",
distinct_id="distinct_id",
)

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

def test_basic_alias(self):
client = self.client
Expand Down Expand Up @@ -819,15 +809,13 @@ def test_advanced_page(self):
"distinct_id",
"https://posthog.com/contact",
{"property": "value"},
{"ip": "192.168.0.1"},
datetime(2014, 9, 3),
"new-uuid",
timestamp=datetime(2014, 9, 3),
uuid="new-uuid",
)

self.assertTrue(success)

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