Skip to content

Commit 4ee6454

Browse files
committed
fix: types and tests
1 parent 2e42334 commit 4ee6454

File tree

3 files changed

+250
-82
lines changed

3 files changed

+250
-82
lines changed

.coverage

0 Bytes
Binary file not shown.

json2xml/dicttoxml.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_unique_id(element: str) -> str:
5959
float,
6060
bool,
6161
numbers.Number,
62-
Sequence[str],
62+
Sequence[Any],
6363
datetime.datetime,
6464
datetime.date,
6565
None,
@@ -97,7 +97,7 @@ def get_xml_type(val: ELEMENT) -> str:
9797
return type(val).__name__
9898

9999

100-
def escape_xml(s: str | numbers.Number) -> str:
100+
def escape_xml(s: str | int | float | numbers.Number) -> str:
101101
"""
102102
Escape a string for use in XML.
103103
@@ -178,7 +178,7 @@ def make_valid_xml_name(key: str, attr: dict[str, Any]) -> tuple[str, dict[str,
178178
return key, attr
179179

180180

181-
def wrap_cdata(s: str | numbers.Number) -> str:
181+
def wrap_cdata(s: str | int | float | numbers.Number) -> str:
182182
"""Wraps a string into CDATA sections"""
183183
s = str(s).replace("]]>", "]]]]><![CDATA[>")
184184
return "<![CDATA[" + s + "]]>"
@@ -505,24 +505,32 @@ def convert_list(
505505

506506
def convert_kv(
507507
key: str,
508-
val: str | numbers.Number,
508+
val: str | int | float | numbers.Number | datetime.datetime | datetime.date,
509509
attr_type: bool,
510-
attr: dict[str, Any] = {},
510+
attr: dict[str, Any] | None = None,
511511
cdata: bool = False,
512512
) -> str:
513-
"""Converts a number or string into an XML element"""
513+
"""Converts a number, string, or datetime into an XML element"""
514+
if attr is None:
515+
attr = {}
514516
key, attr = make_valid_xml_name(key, attr)
515517

518+
# Convert datetime to isoformat string
519+
if hasattr(val, "isoformat") and isinstance(val, (datetime.datetime, datetime.date)):
520+
val = val.isoformat()
521+
516522
if attr_type:
517523
attr["type"] = get_xml_type(val)
518524
attr_string = make_attrstring(attr)
519525
return f"<{key}{attr_string}>{wrap_cdata(val) if cdata else escape_xml(val)}</{key}>"
520526

521527

522528
def convert_bool(
523-
key: str, val: bool, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False
529+
key: str, val: bool, attr_type: bool, attr: dict[str, Any] | None = None, cdata: bool = False
524530
) -> str:
525531
"""Converts a boolean into an XML element"""
532+
if attr is None:
533+
attr = {}
526534
key, attr = make_valid_xml_name(key, attr)
527535

528536
if attr_type:
@@ -532,9 +540,11 @@ def convert_bool(
532540

533541

534542
def convert_none(
535-
key: str, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False
543+
key: str, attr_type: bool, attr: dict[str, Any] | None = None, cdata: bool = False
536544
) -> str:
537545
"""Converts a null value into an XML element"""
546+
if attr is None:
547+
attr = {}
538548
key, attr = make_valid_xml_name(key, attr)
539549

540550
if attr_type:
@@ -544,7 +554,7 @@ def convert_none(
544554

545555

546556
def dicttoxml(
547-
obj: dict[str, Any],
557+
obj: ELEMENT,
548558
root: bool = True,
549559
custom_root: str = "root",
550560
ids: list[int] | None = None,

0 commit comments

Comments
 (0)