2
2
3
3
from collections .abc import Iterable , Iterator , Mapping
4
4
from dataclasses import asdict , dataclass , field , fields , is_dataclass
5
- from datetime import date
5
+ from datetime import date , time , timedelta
6
6
from enum import Enum
7
7
from typing import Any , Literal
8
8
from xml .etree import ElementTree
@@ -27,7 +27,7 @@ def format_as_xml(
27
27
This is useful since LLMs often find it easier to read semi-structured data (e.g. examples) as XML,
28
28
rather than JSON etc.
29
29
30
- Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `date`, `datetime`, `Enum`,
30
+ Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `date`, `datetime`, `time`, `timedelta`, ` Enum`,
31
31
`Mapping`, `Iterable`, `dataclass`, and `BaseModel`.
32
32
33
33
Args:
@@ -104,8 +104,10 @@ def _to_xml(self, value: Any, path: str, tag: str | None = None) -> ElementTree.
104
104
element .text = value .decode (errors = 'ignore' )
105
105
elif isinstance (value , bool | int | float | Enum ):
106
106
element .text = str (value )
107
- elif isinstance (value , date ):
107
+ elif isinstance (value , date | time ):
108
108
element .text = value .isoformat ()
109
+ elif isinstance (value , timedelta ):
110
+ element .text = str (value )
109
111
elif isinstance (value , Mapping ):
110
112
if tag is None and path in self ._element_names :
111
113
element .tag = self ._element_names [path ]
@@ -165,7 +167,7 @@ def _parse_data_structures(
165
167
path : str = '' ,
166
168
):
167
169
"""Parse data structures as dataclasses or Pydantic models to extract element names and attributes."""
168
- if value is None or isinstance (value , (str | int | float | date | bytearray | bytes | bool )):
170
+ if value is None or isinstance (value , (str | int | float | date | time | timedelta | bytearray | bytes | bool )):
169
171
return
170
172
elif isinstance (value , Mapping ):
171
173
for k , v in value .items (): # pyright: ignore[reportUnknownVariableType]
0 commit comments