fix: round-trip timedelta cron offset (#605)#625
Open
sfrangulov wants to merge 1 commit into
Open
Conversation
pydantic serializes a timedelta offset to an ISO-8601 duration string
(e.g. "PT4H"). Because the offset fields are typed as str | timedelta,
re-validating kept the value as a str, so the scheduler later passed it
to ZoneInfo("PT4H") and the schedule broke.
Add a shared parse_cron_offset validator that restores a timedelta from
its serialized duration form while leaving genuine timezone names (e.g.
"US/Eastern") and other values untouched. Apply it as a before-validator
on CronSpec.offset and ScheduledTask.cron_offset (pydantic v1 and v2).
Closes taskiq-python#605
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A
timedeltacron offset breaks after a serialize→deserialize round-trip. pydantic serializes atimedeltato an ISO-8601 duration string (e.g."PT4H"); because the offset fields are typedstr | timedelta, on reload the value stays astr, sois_cron_task_nowtakes the timezone-name branch and callsZoneInfo("PT4H")→ the schedule breaks.Reproduction (from #605):
Fix
Add a shared
parse_cron_offsetvalidator (scheduler/scheduled_task/validators.py) that restores atimedeltafrom its serialized ISO-8601 duration form, while leaving genuine timezone names (e.g."US/Eastern") and all other values untouched — a timezone name is not a valid duration, so it is safely passed through.Applied as a before-validator on:
CronSpec.offsetScheduledTask.cron_offset(pydantic v1 and v2 models)Tests
tests/scheduler/test_cron_offset_roundtrip.py— round-trip forCronSpecandScheduledTaskwithtimedeltaoffsets, plus regression guards that timezone-name offsets are preserved.Verification (local)
tests/scheduler/,tests/cli/scheduler/).ruff,black,mypyclean on changed files.taskiq/result/result.pyand shares the same helper.Closes #605