Skip to content

Commit 00f49e9

Browse files
committed
chore: dict_dump_xml needs to raise an exception when it fails
1 parent 168a49d commit 00f49e9

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

src/planai/utils.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,13 @@ def model_dump_xml(self, root: str = "root"):
8484

8585
def dict_dump_xml(dict: Dict[Any, Any], root: str = "root") -> str:
8686
"""Formats the task as XML."""
87-
try:
88-
xml = dicttoxml.dicttoxml(dict, custom_root=root, attr_type=False)
89-
# Decode bytes to string with utf-8 encoding
90-
xml_str = xml.decode("utf-8")
91-
xml_string = parseString(xml_str).toprettyxml(indent=" ")
92-
# Remove the XML declaration efficiently
93-
if xml_string.startswith("<?xml"):
94-
newline_index = xml_string.find("\n")
95-
if newline_index != -1:
96-
xml_string = xml_string[(newline_index + 1) :]
97-
return xml_string
98-
except Exception as e:
99-
logging.error(f"Failed to convert dictionary to XML: {e}")
100-
# Return a simple valid XML as fallback
101-
return f"<{root}><error>Failed to convert to XML</error></{root}>"
87+
xml = dicttoxml.dicttoxml(dict, custom_root=root, attr_type=False)
88+
# Decode bytes to string with utf-8 encoding
89+
xml_str = xml.decode("utf-8")
90+
xml_string = parseString(xml_str).toprettyxml(indent=" ")
91+
# Remove the XML declaration efficiently
92+
if xml_string.startswith("<?xml"):
93+
newline_index = xml_string.find("\n")
94+
if newline_index != -1:
95+
xml_string = xml_string[(newline_index + 1) :]
96+
return xml_string

tests/planai/test_utils.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,6 @@ def test_complex_types(self):
4949
self.assertIn("<bytes>", result)
5050
self.assertIn("<set>", result)
5151

52-
def test_invalid_input(self):
53-
test_cases = [
54-
("Function value", {"func": lambda x: x}),
55-
("Circular reference", {}),
56-
]
57-
58-
# Create circular reference
59-
d = test_cases[1][1]
60-
d["circular"] = d
61-
62-
for case_name, test_dict in test_cases:
63-
with self.subTest(case_name=case_name):
64-
result = dict_dump_xml(test_dict)
65-
self.assertIsInstance(result, str)
66-
self.assertIn("<error>Failed to convert to XML</error>", result)
67-
6852
def test_custom_root(self):
6953
test_dict = {"key": "value"}
7054
result = dict_dump_xml(test_dict, root="custom")
@@ -82,6 +66,12 @@ def test_unicode_keys(self):
8266
self.assertIn("<키>값</키>", result)
8367
self.assertIn("<ключ>значение</ключ>", result)
8468

69+
def test_random_bytes(self):
70+
test_dict = {"random": b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"}
71+
result = dict_dump_xml(test_dict)
72+
self.assertIn("<random>", result)
73+
self.assertIn("</random>", result)
74+
8575

8676
if __name__ == "__main__":
8777
unittest.main()

0 commit comments

Comments
 (0)