Skip to content

Commit 9821cbb

Browse files
dmartinsteinitzu
authored andcommitted
Consider default arguments for unique_on tasks
1 parent 5b60bc8 commit 9821cbb

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

celery_singleton/singleton.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def generate_lock(self, task_name, task_args=None, task_kwargs=None):
6464
unique_kwargs = {}
6565
else:
6666
sig = inspect.signature(self.run)
67-
bound = sig.bind(*task_args, **task_kwargs).arguments
68-
unique_kwargs = {key: bound[key] for key in unique_on}
67+
bound = sig.bind(*task_args, **task_kwargs)
68+
bound.apply_defaults()
69+
unique_kwargs = {key: bound.arguments[key] for key in unique_on}
6970
unique_args = []
7071
else:
7172
unique_args = task_args

tests/test_singleton.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,34 @@ def unique_on_string_task(a, b=2, c=3, d=4):
288288
assert mock_gen.call_count == 2
289289
assert [list(a) for a in mock_gen.call_args_list] == expected_args
290290

291+
@mock.patch.object(
292+
util, "generate_lock", autospec=True, side_effect=util.generate_lock
293+
)
294+
def test__unique_on_handles_unspecified_default_args(
295+
self, mock_gen, scoped_app, celery_session_worker
296+
):
297+
with scoped_app as app:
298+
299+
@celery_session_worker.app.task(base=Singleton, unique_on="d")
300+
def unique_on_default_task(a, b=2, c=3, d=4):
301+
return a * b * c * d
302+
303+
celery_session_worker.reload() # So task is registered
304+
305+
result = unique_on_default_task.delay(2, b=3, c=4)
306+
307+
result.get()
308+
time.sleep(0.05) # Small delay for on_success
309+
310+
expected_args = [
311+
[
312+
(unique_on_default_task.name, [], {"d": 4}),
313+
{"key_prefix": unique_on_default_task.singleton_config.key_prefix},
314+
]
315+
] * 2
316+
assert mock_gen.call_count == 2
317+
assert [list(a) for a in mock_gen.call_args_list] == expected_args
318+
291319

292320
class TestRaiseOnDuplicateConfig:
293321
def test__default_false(self, scoped_app):

0 commit comments

Comments
 (0)