Skip to content

Commit 778c0b0

Browse files
committed
Prevent tasks from being called directly.
This is likely to be a large foot-gun for new users. Instead, require an explicit `call` / `acall` method.
1 parent b8385ab commit 778c0b0

File tree

2 files changed

+0
-8
lines changed

2 files changed

+0
-8
lines changed

django_tasks/task.py

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
TYPE_CHECKING,
77
Any,
88
Callable,
9-
Coroutine,
109
Dict,
1110
Generic,
1211
Optional,
@@ -129,11 +128,6 @@ async def aget_result(self, result_id: str) -> "TaskResult[T]":
129128

130129
return result
131130

132-
def __call__(
133-
self, *args: P.args, **kwargs: P.kwargs
134-
) -> Union[T, Coroutine[T, None, None]]:
135-
return self.func(*args, **kwargs)
136-
137131
def call(self, *args: P.args, **kwargs: P.kwargs) -> T:
138132
if iscoroutinefunction(self.func):
139133
return async_to_sync(self.func)(*args, **kwargs) # type:ignore[no-any-return]

tests/tests/test_tasks.py

-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,12 @@ async def test_invalid_priority(self) -> None:
134134
await test_tasks.noop_task.using(priority=0).aenqueue()
135135

136136
def test_call_task(self) -> None:
137-
self.assertEqual(test_tasks.calculate_meaning_of_life(), 42)
138137
self.assertEqual(test_tasks.calculate_meaning_of_life.call(), 42)
139138

140139
async def test_call_task_async(self) -> None:
141140
self.assertEqual(await test_tasks.calculate_meaning_of_life.acall(), 42)
142141

143142
async def test_call_async_task(self) -> None:
144-
self.assertIsNone(await test_tasks.noop_task_async()) # type:ignore[func-returns-value]
145143
self.assertIsNone(await test_tasks.noop_task_async.acall())
146144

147145
def test_call_async_task_sync(self) -> None:

0 commit comments

Comments
 (0)