Repository navigation
fix(workflow_scheduler): normalize naive/aware datetime comparison to tz-aware UTC (#12453) - #12457
Merged
Merged
Conversation
… tz-aware UTC (#12453) Naive datetime objects passed as scheduled_time/new_time (e.g. datetime.now() from a caller) were never normalized to UTC, unlike the string path which already went through parse_utc_iso/_parse_time_string. Later comparisons against tz-aware now (get_scheduler_status, list_scheduled_workflows sort, _process_scheduled_workflows, _calculate_priority_score) raised TypeError: can't compare offset-naive and offset-aware datetimes. Add _ensure_utc_aware() following the parse_utc_iso convention (naive is assumed UTC) and apply it to the datetime-object branch of _parse_schedule_params and reschedule_workflow.
Contributor
✅ SSOT Configuration Compliance: Passing🎉 No hardcoded values detected that have SSOT config equivalents! |
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.
Thinking Path
Bug #12453 (surfaced by #12452 making
workflow_scheduler_e2e_test.pyrunnable):services/workflow_scheduler.pyraisesTypeError: can't compare offset-naive and offset-aware datetimesat runtime.Traced every
scheduled_timeorigin: string inputs already go throughparse_utc_iso/_parse_time_string, both of which return tz-aware datetimes (naive strings are assumed UTC, matching the codebase convention documented inautobot_shared/time_utils.py). The gap: when a caller passes adatetimeobject directly (e.g.datetime.now(), which is naive) toschedule_workflow()orreschedule_workflow(), it was stored as-is with no normalization. Later comparisons againstdatetime.now(tz=timezone.utc)inget_scheduler_status()(line 758),list_scheduled_workflows()'s sort key,_process_scheduled_workflows(), and_calculate_priority_score()then mix naive and aware values and raiseTypeError.What Changed
_ensure_utc_aware(value: datetime) -> datetimehelper inautobot-backend/workflow_scheduler.py, following the same "naive is assumed UTC" convention asautobot_shared.time_utils.parse_utc_iso.datetime-object branch of_parse_schedule_params(used byschedule_workflow) andreschedule_workflow, so every code path that stores ascheduled_time/new_timenow guarantees a tz-aware UTC value — whether the input was a string or a naivedatetimeobject._load_workflows/ScheduledWorkflow.from_dictalready useparse_utc_iso, and every otherdatetime.now(...)call in the file already passestz=timezone.utc.Verification
Reverted the fix locally (via
git apply -R) to confirm it reproduces the reported bug, then re-applied:Before fix — 2 failures, both
TypeError: can't compare offset-naive and offset-aware datetimes(one atworkflow_scheduler.py:745insidelist_scheduled_workflows's sort, one atworkflow_scheduler.py:758insideget_scheduler_status, exactly as described in #12453):After fix:
black --checkandflake8 --max-line-length=120on the changed file: clean.Model Used
Claude Sonnet 5
Closes #12453