#36633 new Uncategorized

TaskBackend run_after validation assumes datetime when USE_TZ=True

Reported by: Chris M Owned by:
Component: Tasks Version: 6.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

        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.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top