@@ -823,9 +823,13 @@ def test_nested_scopes_with_tags(sentry_init, capture_envelopes):
823823 (envelope ,) = envelopes
824824 transaction = envelope .items [0 ].get_transaction_event ()
825825
826- assert transaction ["tags" ] == {"isolation_scope1" : 1 , "current_scope2" : 1 , "trx" : 1 }
827- assert transaction ["spans" ][0 ]["tags" ] == ApproxDict ({"a" : 1 })
828- assert transaction ["spans" ][1 ]["tags" ] == ApproxDict ({"b" : 1 })
826+ assert transaction ["tags" ] == {
827+ "isolation_scope1" : "1" ,
828+ "current_scope2" : "1" ,
829+ "trx" : "1" ,
830+ }
831+ assert transaction ["spans" ][0 ]["tags" ] == ApproxDict ({"a" : "1" })
832+ assert transaction ["spans" ][1 ]["tags" ] == ApproxDict ({"b" : "1" })
829833
830834
831835def test_should_send_default_pii_true (sentry_init ):
@@ -931,3 +935,107 @@ def test_root_span(sentry_init):
931935 assert sentry_sdk .get_current_scope ().root_span == root_span
932936
933937 assert sentry_sdk .get_current_scope ().root_span is None
938+
939+
940+ @pytest .mark .parametrize (
941+ ("key" , "value" , "expected" ),
942+ [
943+ ("int" , 123 , "123" ),
944+ ("float" , 123.456 , "123.456" ),
945+ ("bool_true" , True , "True" ),
946+ ("bool_false" , False , "False" ),
947+ ("none" , None , "None" ),
948+ ("list" , [1 , 2 , 3 ], "[1, 2, 3]" ),
949+ ("dict" , {"key" : "value" }, "{'key': 'value'}" ),
950+ ("already_string" , "test" , "test" ),
951+ ],
952+ )
953+ def test_set_tag_converts_to_string (key , value , expected ):
954+ """Test that set_tag converts various types to strings."""
955+ scope = Scope ()
956+ scope .set_tag (key , value )
957+
958+ event = scope .apply_to_event ({}, {})
959+ tags = event .get ("tags" , {})
960+
961+ assert tags [key ] == expected , f"Tag { key } was not converted properly"
962+
963+
964+ def test_set_tags_converts_to_string ():
965+ """Test that set_tags converts all values to strings."""
966+ scope = Scope ()
967+
968+ scope .set_tags (
969+ {
970+ "int" : 123 ,
971+ "float" : 123.456 ,
972+ "bool" : True ,
973+ "none" : None ,
974+ "list" : [1 , 2 , 3 ],
975+ "string" : "test" ,
976+ }
977+ )
978+
979+ event = scope .apply_to_event ({}, {})
980+ tags = event .get ("tags" , {})
981+
982+ assert tags ["int" ] == "123"
983+ assert tags ["float" ] == "123.456"
984+ assert tags ["bool" ] == "True"
985+ assert tags ["none" ] == "None"
986+ assert tags ["list" ] == "[1, 2, 3]"
987+ assert tags ["string" ] == "test"
988+
989+
990+ def test_set_tag_handles_conversion_failure ():
991+ """Test that set_tag handles objects that fail to convert to string."""
992+ scope = Scope ()
993+
994+ # Create an object that raises an exception when str() is called
995+ class BadObject :
996+ def __str__ (self ):
997+ raise Exception ("Cannot convert to string" )
998+
999+ def __repr__ (self ):
1000+ return "BadObject()"
1001+
1002+ bad_obj = BadObject ()
1003+
1004+ # This should not raise an exception
1005+ scope .set_tag ("bad_object" , bad_obj )
1006+
1007+ # The tag should be set with the repr value
1008+ event = scope .apply_to_event ({}, {})
1009+ tags = event .get ("tags" , {})
1010+
1011+ assert tags ["bad_object" ] == "BadObject()" , "Tag should be set with repr value"
1012+
1013+
1014+ def test_set_tags_handles_conversion_failure ():
1015+ """Test that set_tags handles objects that fail to convert to string."""
1016+ scope = Scope ()
1017+
1018+ # Create an object that raises an exception when str() is called
1019+ class BadObject :
1020+ def __str__ (self ):
1021+ raise Exception ("Cannot convert to string" )
1022+
1023+ def __repr__ (self ):
1024+ return "BadObject()"
1025+
1026+ bad_obj = BadObject ()
1027+
1028+ scope .set_tags (
1029+ {
1030+ "good_tag1" : "value1" ,
1031+ "bad_tag" : bad_obj ,
1032+ "good_tag2" : 123 ,
1033+ }
1034+ )
1035+
1036+ event = scope .apply_to_event ({}, {})
1037+ tags = event .get ("tags" , {})
1038+
1039+ assert tags ["good_tag1" ] == "value1"
1040+ assert tags ["bad_tag" ] == "BadObject()" , "Tag should be set with repr value"
1041+ assert tags ["good_tag2" ] == "123"
0 commit comments