[Django] #36633: TaskBackend run_after validation assumes datetime when USE_TZ=True

4 views
Skip to first unread message

Django

unread,
Sep 30, 2025, 11:10:36 PM (22 hours ago) Sep 30
to django-...@googlegroups.com
#36633: TaskBackend run_after validation assumes datetime when USE_TZ=True
-------------------------+-----------------------------------------
Reporter: Chris M | Type: Uncategorized
Status: new | Component: Tasks
Version: 6.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------+-----------------------------------------
I’ve been working on an implementation of a TaskBackend using Google Cloud
Tasks. I ran across a bit of confusion regarding the {{{run_after}}}
parameter validation.

The DEP originally referenced that the value could be a datetime or a
timedelta. The released implementation looks like it only documents it as
a datetime, so I’m not sure if it was meant to support timedelta. However,
I tried using a timedelta with the {{{USE_TZ=True}}} and the validation in
BaseTaskBackend assumes that the run_after value is a {{{datetime}}} and I
got an error

Calling code:
{{{my_task.using(run_after=timedelta(seconds=10)).enqueue()}}}

Error: {{{AttributeError: 'datetime.timedelta' object has no attribute
'utcoffset'}}}

If I run the code using a {{{timedelta}}} and with {{{USE_TZ=False}}} then
the error isn't thrown and my backend can process it.

The code throwing the error in the {{{BaseTaskBackend}}} is

{{{#!python
if (
settings.USE_TZ
and task.run_after is not None
and not timezone.is_aware(task.run_after)
):
raise InvalidTask("run_after must be an aware datetime.")
}}}

I think it would be best for this condition to verify that the provided
{{{run_after}}} is a {{{datetime}}} before calling
{{{timezone.is_aware}}}. I'm hoping for some confirmation if this is the
right way to proceed.

I think it would be great to support {{{timedelta}}} directly as well by
updating the type annotations on {{{Task}}}, but I couldn't determine
through the PR history if this was removed during implementation.
--
Ticket URL: <https://code.djangoproject.com/ticket/36633>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages