From 2d7903a18defe77a5b5105d93ae6da38dec92c19 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Mon, 7 Mar 2022 23:51:12 +0100 Subject: [PATCH 1/5] fix(serializer): Make sentry_repr dunder method to avoid mock problems --- sentry_sdk/serializer.py | 7 ++++--- tests/test_serializer.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/serializer.py b/sentry_sdk/serializer.py index df6a9053c1..653ce2ea52 100644 --- a/sentry_sdk/serializer.py +++ b/sentry_sdk/serializer.py @@ -273,6 +273,10 @@ def _serialize_node_impl( if result is not NotImplemented: return _flatten_annotated(result) + sentry_repr = getattr(type(obj), "__sentry_repr__", None) + if callable(sentry_repr): + return sentry_repr(obj) + if obj is None or isinstance(obj, (bool, number_types)): if should_repr_strings or ( isinstance(obj, float) and (math.isinf(obj) or math.isnan(obj)) @@ -281,9 +285,6 @@ def _serialize_node_impl( else: return obj - elif callable(getattr(obj, "sentry_repr", None)): - return obj.sentry_repr() - elif isinstance(obj, datetime): return ( text_type(format_timestamp(obj)) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 503bc14fb2..d67705b3f9 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -1,6 +1,7 @@ import sys import pytest +from unittest import mock from sentry_sdk.serializer import serialize @@ -68,8 +69,19 @@ def test_serialize_sets(extra_normalizer): def test_serialize_custom_mapping(extra_normalizer): class CustomReprDict(dict): - def sentry_repr(self): + def __sentry_repr__(self): return "custom!" result = extra_normalizer(CustomReprDict(one=1, two=2)) assert result == "custom!" + + +def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): + """ + Adding the __sentry_repr__ magic method check in the serializer + shouldn't mess with how mock works. This broke some stuff when we added + sentry_repr without the dunders. + """ + m = mock.Mock() + result = extra_normalizer(m) + assert len(m.mock_calls) == 0 From f0179f740b868ccf6150970126ae2c3edc6b658f Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 8 Mar 2022 00:11:14 +0100 Subject: [PATCH 2/5] Move back down --- sentry_sdk/serializer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/serializer.py b/sentry_sdk/serializer.py index 653ce2ea52..134528cd9a 100644 --- a/sentry_sdk/serializer.py +++ b/sentry_sdk/serializer.py @@ -274,8 +274,6 @@ def _serialize_node_impl( return _flatten_annotated(result) sentry_repr = getattr(type(obj), "__sentry_repr__", None) - if callable(sentry_repr): - return sentry_repr(obj) if obj is None or isinstance(obj, (bool, number_types)): if should_repr_strings or ( @@ -285,6 +283,9 @@ def _serialize_node_impl( else: return obj + elif callable(sentry_repr): + return sentry_repr(obj) + elif isinstance(obj, datetime): return ( text_type(format_timestamp(obj)) From 6bed616edf5ff7bf640fc15faedd6bd59382b4a8 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 8 Mar 2022 00:13:38 +0100 Subject: [PATCH 3/5] Lint --- tests/test_serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index d67705b3f9..afefb44ece 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -83,5 +83,5 @@ def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): sentry_repr without the dunders. """ m = mock.Mock() - result = extra_normalizer(m) + extra_normalizer(m) assert len(m.mock_calls) == 0 From 5bc4c8632b9ec7e18f37771c5840641db305114e Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 8 Mar 2022 10:49:18 +0100 Subject: [PATCH 4/5] No mock in 2.7 --- tests/test_serializer.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index afefb44ece..6b45d49ba2 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -1,7 +1,5 @@ import sys - import pytest -from unittest import mock from sentry_sdk.serializer import serialize @@ -76,12 +74,18 @@ def __sentry_repr__(self): assert result == "custom!" -def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): - """ - Adding the __sentry_repr__ magic method check in the serializer - shouldn't mess with how mock works. This broke some stuff when we added - sentry_repr without the dunders. - """ - m = mock.Mock() - extra_normalizer(m) - assert len(m.mock_calls) == 0 +try: + from unittest import mock +except ImportError: + pass +else: + + def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): + """ + Adding the __sentry_repr__ magic method check in the serializer + shouldn't mess with how mock works. This broke some stuff when we added + sentry_repr without the dunders. + """ + m = mock.Mock() + extra_normalizer(m) + assert len(m.mock_calls) == 0 From 594a00a5192d2133516b752ff846f6e32b18ebf2 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 8 Mar 2022 11:01:47 +0100 Subject: [PATCH 5/5] Use importorskip --- tests/test_serializer.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 6b45d49ba2..1cc20c4b4a 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -74,18 +74,13 @@ def __sentry_repr__(self): assert result == "custom!" -try: - from unittest import mock -except ImportError: - pass -else: - - def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): - """ - Adding the __sentry_repr__ magic method check in the serializer - shouldn't mess with how mock works. This broke some stuff when we added - sentry_repr without the dunders. - """ - m = mock.Mock() - extra_normalizer(m) - assert len(m.mock_calls) == 0 +def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): + """ + Adding the __sentry_repr__ magic method check in the serializer + shouldn't mess with how mock works. This broke some stuff when we added + sentry_repr without the dunders. + """ + mock = pytest.importorskip("unittest.mock") + m = mock.Mock() + extra_normalizer(m) + assert len(m.mock_calls) == 0