8000 fix(serializer): Make sentry_repr dunder method to avoid mock problem… · tinylambda/sentry-python@a14c127 · GitHub
[go: up one dir, main page]

Skip to content

Commit a14c127

Browse files
authored
fix(serializer): Make sentry_repr dunder method to avoid mock problems (getsentry#1364)
1 parent b63b5ae commit a14c127

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

sentry_sdk/serializer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ def _serialize_node_impl(
273273
if result is not NotImplemented:
274274
return _flatten_annotated(result)
275275

276+
sentry_repr = getattr(type(obj), "__sentry_repr__", None)
277+
276278
if obj is None or isinstance(obj, (bool, number_types)):
277279
if should_repr_strings or (
278280
isinstance(obj, float) and (math.isinf(obj) or math.isnan(obj))
@@ -281,8 +283,8 @@ def _serialize_node_impl(
281283
else:
282284
return obj
283285

284-
elif callable(getattr(obj, "sentry_repr", None)):
285-
return obj.sentry_repr()
286+
elif callable(sentry_repr):
287+
return sentry_repr(obj)
286288

287289
elif isinstance(obj, datetime):
288290
return (

tests/test_serializer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
32
import pytest
43

54
from sentry_sdk.serializer import serialize
@@ -68,8 +67,20 @@ def test_serialize_sets(extra_normalizer):
6867

6968
def test_serialize_custom_mapping(extra_normalizer):
7069
class CustomReprDict(dict):
71-
def sentry_repr(self):
70+
def __sentry_repr__(self):
7271
return "custom!"
7372

7473
result = extra_normalizer(CustomReprDict(one=1, two=2))
7574
assert result == "custom!"
75+
76+
77+
def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer):
78+
"""
79+
Adding the __sentry_repr__ magic method check in the serializer
80+
shouldn't mess with how mock works. This broke some stuff when we added
81+
sentry_repr without the dunders.
82+
"""
83+
mock = pytest.importorskip("unittest.mock")
84+
m = mock.Mock()
85+
extra_normalizer(m)
86+
assert len(m.mock_calls) == 0

0 commit comments

Comments
 (0)
0