Skip to content

Commit 157d53c

Browse files
fix: notify users if using incompatible pydantic & python combo
1 parent 46f55e0 commit 157d53c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

posthog/test/test_utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import time
23
import unittest
34
from dataclasses import dataclass
@@ -122,11 +123,29 @@ class NestedModel(BaseModel):
122123
"bar": 2,
123124
"baz": None,
124125
}
125-
assert utils.clean(ModelV1(foo=1, bar="2")) == {"foo": 1, "bar": "2"}
126+
# Pydantic V1 is not compatible with Python 3.14+
127+
if sys.version_info < (3, 14):
128+
assert utils.clean(ModelV1(foo=1, bar="2")) == {"foo": 1, "bar": "2"}
126129
assert utils.clean(NestedModel(foo=ModelV2(foo="1", bar=2, baz="3"))) == {
127130
"foo": {"foo": "1", "bar": 2, "baz": "3"}
128131
}
129132

133+
@unittest.skipIf(sys.version_info < (3, 14), "Test only relevant for Python 3.14+")
134+
def test_clean_pydantic_v1_warning_on_python_314(self):
135+
"""Verify that Pydantic V1 models emit a warning on Python 3.14+"""
136+
class ModelV1(BaseModelV1):
137+
foo: int
138+
bar: str
139+
140+
with self.assertLogs("posthog", level="WARNING") as cm:
141+
result = utils.clean(ModelV1(foo=1, bar="2"))
142+
# V1 models return empty dict on Python 3.14+
143+
assert result == {}
144+
145+
# Verify the warning was emitted
146+
assert len(cm.output) == 1
147+
assert "Pydantic V1 models are not compatible with Python 3.14+" in cm.output[0]
148+
130149
def test_clean_pydantic_like_class(self) -> None:
131150
class Dummy:
132151
def model_dump(self, required_param: str) -> dict:

posthog/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,15 @@ def clean(item):
7171
item = item.model_dump()
7272
# v1
7373
elif hasattr(item, "dict") and callable(item.dict):
74-
item = item.dict()
74+
result = item.dict()
75+
if not result and sys.version_info >= (3, 14):
76+
log.warning(
77+
"Pydantic V1 models are not compatible with Python 3.14+. "
78+
"Please upgrade to Pydantic V2. Data may not be serialized correctly."
79+
)
80+
item = result
7581
except TypeError as e:
7682
log.debug(f"Could not serialize Pydantic-like model: {e}")
77-
pass
7883
if isinstance(item, dict):
7984
return _clean_dict(item)
8085
if is_dataclass(item) and not isinstance(item, type):

0 commit comments

Comments
 (0)