Skip to content

Accept integer seconds when deserializing datetime.timedelta - #72710

Closed
CaptainAni187 wants to merge 2 commits into
apache:mainfrom
CaptainAni187:fix/serde-timedelta-int-payload
Closed

CaptainAni187 wants to merge 2 commits into
apache:mainfrom
CaptainAni187:fix/serde-timedelta-int-payload

Conversation

@CaptainAni187

Copy link
Copy Markdown

Closes: #72319

A datetime.timedelta payload whose seconds arrive as an int fails to deserialize:

deserialize({"__classname__": "datetime.timedelta", "__version__": 2, "__data__": 2700})
# TypeError: unknown date/time format datetime.timedelta

2700.0 and "2700" both work. The exception is unhandled during scheduler DAG-run creation, so it takes the scheduler process down.

Cause

The deserializer accepts only str | float for timedelta:

if cls is datetime.timedelta and isinstance(data, str | float):
    return datetime.timedelta(seconds=float(data))

isinstance(2700, float) is False, so an integral payload falls through to the TypeError at the end of the function.

Change

Widen the check to str | int | float.

Two things make this the natural fix rather than a special case:

  • The datetime branch a few lines above already accepts int | float, with a comment explaining it exists so payloads written by a different producer can be read back through serde. This is the same situation one type down.
  • The value goes through float(data) either way, so an int is handled losslessly. Rejecting it was arbitrary rather than protective.

On where the int comes from

I could not reproduce a producer inside this repo that emits the bare int — serialize() writes total_seconds() as a float, BaseSerialization does the same, and a plain JSON round-trip preserves 2700.0. So I am not claiming to have found the exact path from the reported traceback.

What is clear is that the value is reachable: any producer that drops a trailing .0 (common across JSON encoders and other languages) yields 2700, and the deadline-interval code this was reported from deals in integer seconds by design — VariableInterval documents its variable as "interpreted as seconds" and builds the timedelta via int(value).

Given a one-character-class widening against an unhandled scheduler crash, accepting the int seems clearly right regardless of which producer emitted it. Happy to dig further if you would rather fix this at the producer instead.

Tests

Adds test_deserialize_timedelta_seconds, parametrised over int, float and str. On main the int case fails with the reported TypeError while float and str pass, so the test pins the specific broken path rather than the whole branch.

tests/task_sdk/serde/ goes from 144 to 147 passed. Nine tests in that directory fail identically before and after this change in my environment, from missing optional dependencies (pyarrow, deltalake, airflow.providers.cncf); they are unrelated to this change.

ruff check and ruff format are clean at the pinned 0.16.4.

The serde datetime deserializer accepted only str and float for
datetime.timedelta, so a payload carrying whole seconds as an int raised
`TypeError: unknown date/time format datetime.timedelta`. The exception is
unhandled during scheduler DAG-run creation, which terminates the process.

`serialize` writes `total_seconds()` as a float, but a producer that drops
the trailing `.0` yields a bare int. The datetime branch a few lines above
already accepts `int | float` for the same reason, and the value is passed
through `float(data)` regardless, so widening the check is consistent and
lossless.
@CaptainAni187

Copy link
Copy Markdown
Author

Closing this — it's already fixed upstream. #72326 landed the same change (timedelta deserialization now accepts int payloads), so there's nothing left here. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime.timedelta serde cannot deserialize integer payloads, crashing scheduler during DeadlineAlert DAG-run creation

1 participant