Skip to content

Commit a88dcf6

Browse files
clean up Cursor's solution
1 parent 5c38d51 commit a88dcf6

File tree

7 files changed

+137
-285
lines changed

7 files changed

+137
-285
lines changed

CHANGES_SUMMARY.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

PR_SUMMARY.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

sentry_sdk/scope.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ def set_tags(self, tags):
870870
This method only modifies tag keys in the `tags` mapping passed to the method.
871871
`scope.set_tags({})` is, therefore, a no-op.
872872
873-
:param tags: A mapping of tag keys to tag values to set. Values will be converted to strings.
873+
:param tags: A mapping of tag keys to tag values to set. Values will be
874+
converted to strings.
874875
"""
875876
for key, value in tags.items():
876877
self.set_tag(key, value)

sentry_sdk/tracing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,16 @@ def iter_headers(self):
12701270

12711271
def set_tag(self, key, value):
12721272
# type: (str, Any) -> None
1273-
self._tags[key] = safe_str(value)
1273+
pass
1274+
1275+
def set_data(self, key, value):
1276+
# type: (str, Any) -> None
1277+
pass
1278+
1279+
def set_status(self, value):
1280+
# type: (str) -> None
1281+
pass
1282+
12741283
def set_http_status(self, http_status):
12751284
# type: (int) -> None
12761285
pass

tests/test_api.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,48 +190,50 @@ def test_set_tags(sentry_init, capture_events):
190190
}, "Updating tags with empty dict changed tags"
191191

192192

193-
def test_set_tag_converts_to_string(sentry_init, capture_events):
193+
@pytest.mark.parametrize(
194+
("key", "value", "expected"),
195+
[
196+
("int", 123, "123"),
197+
("float", 123.456, "123.456"),
198+
("bool", True, "True"),
199+
("none", None, "None"),
200+
("list", [1, 2, 3], "[1, 2, 3]"),
201+
],
202+
)
203+
def test_set_tag_converts_to_string(sentry_init, capture_events, key, value, expected):
194204
"""Test that the api.set_tag function converts values to strings."""
195205
sentry_init()
196206
events = capture_events()
197207

198-
# Test various types
199-
set_tag("int", 123)
200-
set_tag("float", 123.456)
201-
set_tag("bool", True)
202-
set_tag("none", None)
203-
set_tag("list", [1, 2, 3])
204-
208+
set_tag(key, value)
205209
raise_and_capture()
206-
207-
(*_, event) = events
210+
211+
(event,) = events
208212
tags = event.get("tags", {})
209-
210-
assert tags["int"] == "123"
211-
assert tags["float"] == "123.456"
212-
assert tags["bool"] == "True"
213-
assert tags["none"] == "None"
214-
assert tags["list"] == "[1, 2, 3]"
213+
214+
assert tags[key] == expected
215215

216216

217217
def test_set_tags_converts_to_string(sentry_init, capture_events):
218218
"""Test that the api.set_tags function converts values to strings."""
219219
sentry_init()
220220
events = capture_events()
221221

222-
set_tags({
223-
"int": 456,
224-
"float": 789.012,
225-
"bool": False,
226-
"tuple": (1, 2, 3),
227-
"string": "already_string",
228-
})
229-
222+
set_tags(
223+
{
224+
"int": 456,
225+
"float": 789.012,
226+
"bool": False,
227+
"tuple": (1, 2, 3),
228+
"string": "already_string",
229+
}
230+
)
231+
230232
raise_and_capture()
231-
233+
232234
(*_, event) = events
233235
tags = event.get("tags", {})
234-
236+
235237
assert tags["int"] == "456"
236238
assert tags["float"] == "789.012"
237239
assert tags["bool"] == "False"

tests/test_scope.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,9 @@ def test_last_event_id_cleared(sentry_init):
907907
assert Scope.last_event_id() is None, "last_event_id should be cleared"
908908

909909

910-
def test_set_tag_converts_to_string():
911-
"""Test that set_tag converts various types to strings."""
912-
scope = Scope()
913-
914-
# Test various types
915-
test_cases = [
910+
@pytest.mark.parametrize(
911+
("key", "value", "expected"),
912+
[
916913
("int", 123, "123"),
917914
("float", 123.456, "123.456"),
918915
("bool_true", True, "True"),
@@ -921,34 +918,37 @@ def test_set_tag_converts_to_string():
921918
("list", [1, 2, 3], "[1, 2, 3]"),
922919
("dict", {"key": "value"}, "{'key': 'value'}"),
923920
("already_string", "test", "test"),
924-
]
925-
926-
for key, value, expected in test_cases:
927-
scope.set_tag(key, value)
928-
921+
],
922+
)
923+
def test_set_tag_converts_to_string(key, value, expected):
924+
"""Test that set_tag converts various types to strings."""
925+
scope = Scope()
926+
scope.set_tag(key, value)
927+
929928
event = scope.apply_to_event({}, {})
930929
tags = event.get("tags", {})
931-
932-
for key, value, expected in test_cases:
933-
assert tags[key] == expected, f"Tag {key} was not converted properly"
930+
931+
assert tags[key] == expected, f"Tag {key} was not converted properly"
934932

935933

936934
def test_set_tags_converts_to_string():
937935
"""Test that set_tags converts all values to strings."""
938936
scope = Scope()
939-
940-
scope.set_tags({
941-
"int": 123,
942-
"float": 123.456,
943-
"bool": True,
944-
"none": None,
945-
"list": [1, 2, 3],
946-
"string": "test",
947-
})
948-
937+
938+
scope.set_tags(
939+
{
940+
"int": 123,
941+
"float": 123.456,
942+
"bool": True,
943+
"none": None,
944+
"list": [1, 2, 3],
945+
"string": "test",
946+
}
947+
)
948+
949949
event = scope.apply_to_event({}, {})
950950
tags = event.get("tags", {})
951-
951+
952952
assert tags["int"] == "123"
953953
assert tags["float"] == "123.456"
954954
assert tags["bool"] == "True"
@@ -960,52 +960,52 @@ def test_set_tags_converts_to_string():
960960
def test_set_tag_handles_conversion_failure():
961961
"""Test that set_tag handles objects that fail to convert to string."""
962962
scope = Scope()
963-
963+
964964
# Create an object that raises an exception when str() is called
965965
class BadObject:
966966
def __str__(self):
967967
raise Exception("Cannot convert to string")
968-
968+
969+
def __repr__(self):
970+
return "BadObject()"
971+
969972
bad_obj = BadObject()
970-
973+
971974
# This should not raise an exception
972975
scope.set_tag("bad_object", bad_obj)
973-
974-
# The tag should not be set
975-
event = scope.apply_to_event({}, {})
976-
tags = event.get("tags", {})
977-
978-
assert "bad_object" not in tags, "Tag with failed conversion should not be set"
979-
980-
# Other tags should still work
981-
scope.set_tag("good_tag", "value")
976+
977+
# The tag should be set with the repr value
982978
event = scope.apply_to_event({}, {})
983979
tags = event.get("tags", {})
984-
985-
assert tags["good_tag"] == "value"
980+
981+
assert tags["bad_object"] == "BadObject()", "Tag should be set with repr value"
986982

987983

988984
def test_set_tags_handles_conversion_failure():
989985
"""Test that set_tags handles objects that fail to convert to string."""
990986
scope = Scope()
991-
987+
992988
# Create an object that raises an exception when str() is called
993989
class BadObject:
994990
def __str__(self):
995991
raise Exception("Cannot convert to string")
996-
992+
993+
def __repr__(self):
994+
return "BadObject()"
995+
997996
bad_obj = BadObject()
998-
999-
# This should not raise an exception and should set the convertible tags
1000-
scope.set_tags({
1001-
"good_tag1": "value1",
1002-
"bad_tag": bad_obj,
1003-
"good_tag2": 123,
1004-
})
1005-
997+
998+
scope.set_tags(
999+
{
1000+
"good_tag1": "value1",
1001+
"bad_tag": bad_obj,
1002+
"good_tag2": 123,
1003+
}
1004+
)
1005+
10061006
event = scope.apply_to_event({}, {})
10071007
tags = event.get("tags", {})
1008-
1008+
10091009
assert tags["good_tag1"] == "value1"
1010-
assert "bad_tag" not in tags
1010+
assert tags["bad_tag"] == "BadObject()", "Tag should be set with repr value"
10111011
assert tags["good_tag2"] == "123"

0 commit comments

Comments
 (0)