Opened 4 months ago
Closed 10 days ago
#36816 closed New feature (fixed)
Allow **kwargs in @task decorator to support custom Task subclasses
| Reported by: | Pietro | Owned by: | Nilesh Pahari |
|---|---|---|---|
| Component: | Tasks | Version: | dev |
| Severity: | Normal | Keywords: | task |
| Cc: | Nilesh Pahari, Jake Howard | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
Currently, the @task decorator accepts a fixed set of parameters and passes only those to task_class:
Problem: When using a custom backend with a custom task_class that accepts additional parameters (e.g., max_retries, timeout), there's no way to pass those through the decorator.
def task(
function=None,
*,
priority=DEFAULT_TASK_PRIORITY,
queue_name=DEFAULT_TASK_QUEUE_NAME,
backend=DEFAULT_TASK_BACKEND_ALIAS,
takes_context=False,
):
# ...
return task_backends[backend].task_class(
priority=priority,
func=f,
queue_name=queue_name,
backend=backend,
takes_context=takes_context,
run_after=None
)
Proposed solution: Add kwargs to the decorator signature and pass it through to task_class:
def task(
function=None,
*,
priority=DEFAULT_TASK_PRIORITY,
queue_name=DEFAULT_TASK_QUEUE_NAME,
backend=DEFAULT_TASK_BACKEND_ALIAS,
takes_context=False,
**kwargs,
):
def wrapper(f):
return task_backends[backend].task_class(
priority=priority,
func=f,
queue_name=queue_name,
backend=backend,
takes_context=takes_context,
run_after=None,
**kwargs,
)
Use case example:
class MyTask(Task):
def __init__(self, *, max_retries=3, timeout=300, **kwargs):
super().__init__(**kwargs)
self.max_retries = max_retries
self.timeout = timeout
# With the proposed change:
@task(backend="my_backend", max_retries=5, timeout=600)
def my_task():
pass
This change is backwards compatible and aligns with Django's common extensibility patterns.
Change History (18)
comment:2 by , 4 months ago
| Type: | Bug → New feature |
|---|
comment:3 by , 4 months ago
| Has patch: | unset |
|---|
comment:4 by , 4 months ago
| Cc: | added |
|---|
comment:5 by , 4 months ago
| Cc: | added |
|---|
comment:6 by , 4 months ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:7 by , 4 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:8 by , 4 months ago
| Version: | 6.0 → dev |
|---|
comment:9 by , 4 months ago
| Has patch: | set |
|---|
comment:11 by , 6 weeks ago
| Needs documentation: | set |
|---|
comment:12 by , 5 weeks ago
| Needs documentation: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
comment:13 by , 5 weeks ago
| Triage Stage: | Ready for checkin → Accepted |
|---|
comment:14 by , 5 weeks ago
| Needs documentation: | set |
|---|
comment:15 by , 13 days ago
| Needs documentation: | unset |
|---|
comment:16 by , 11 days ago
| Needs documentation: | set |
|---|
comment:17 by , 11 days ago
| Needs documentation: | unset |
|---|
comment:18 by , 11 days ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
This sounds like a reasonable change to me.
As for the solution, my suggestion would be to drop all explicit arguments, and only accept
**kwargsin the@taskdecorator - instead moving the defaults to theTaskclass itself. That also means that, for whatever reason, a customTaskcan override defaults.