Skip to content

Commit 4624c6d

Browse files
committed
Support datetime.time and timedelta as task input and output
... or anywhere where JSON serialization is needed.
1 parent 048b217 commit 4624c6d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

docs/source/changelog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

3-
## 0.12.1 (2025-10-DD)
3+
## 0.12.2 (2025-10-DD)
4+
- Support datetime.time and timedelta as task input and output (or anywhere where JSON serialization is needed).
5+
6+
## 0.12.1 (2025-10-10)
47
- Create all metadata tables even if some metadata tables already exist.
58
This fixes problems with conditional need for sync_views table.
69
- Make table hooks work even without ConfigContext (see example_mssql/download_parquet_files.py)

src/pydiverse/pipedag/util/json.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Type(str, Enum):
4444
PATHLIB_PATH = "pathlib:path"
4545
DT_DATE = "dt:date"
4646
DT_DATETIME = "dt:datetime"
47+
DT_TIME = "dt:time"
48+
DT_TIMEDELTA = "dt:timedelta"
4749

4850
def __str__(self):
4951
return self.value
@@ -142,6 +144,19 @@ def json_default(o):
142144
TYPE_KEY: Type.DT_DATE,
143145
"date": o.isoformat(),
144146
}
147+
if isinstance(o, dt.time):
148+
return {
149+
TYPE_KEY: Type.DT_TIME,
150+
"time": o.isoformat(),
151+
}
152+
if isinstance(o, dt.timedelta):
153+
return {
154+
TYPE_KEY: Type.DT_TIMEDELTA,
155+
"days": o.days,
156+
"seconds": o.seconds,
157+
"microseconds": o.microseconds,
158+
# docstring of timedelta says: Representation: (days, seconds, microseconds).
159+
}
145160
if get_origin(o) is not None:
146161
# must be GenericAlias
147162
# somehow isinstance(o, GenericAlias) did not work reliably
@@ -270,6 +285,10 @@ def get_stage(name: str | None):
270285
return Path(d["path"])
271286
if type_ == Type.DT_DATE:
272287
return dt.date.fromisoformat(d["date"])
288+
if type_ == Type.DT_TIME:
289+
return dt.time.fromisoformat(d["time"])
290+
if type_ == Type.DT_TIMEDELTA:
291+
return dt.timedelta(d["days"], d["seconds"], d["microseconds"])
273292
if type_ == Type.DT_DATETIME:
274293
return dt.datetime.fromisoformat(d["datetime"])
275294
if type_ == Type.DATA_CLASS:

0 commit comments

Comments
 (0)