Skip to content

Commit 3aaa753

Browse files
tothegumpauvipy
authored andcommitted
Chain with one task should run as expected (celery#4730)
* bugfix: Chain with one task * Fixes celery#4498 * add unit_test
1 parent d178dbb commit 3aaa753

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

celery/canvas.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,9 @@ class chain(_chain):
779779
def __new__(cls, *tasks, **kwargs):
780780
# This forces `chain(X, Y, Z)` to work the same way as `X | Y | Z`
781781
if not kwargs and tasks:
782-
if len(tasks) == 1 and is_list(tasks[0]):
783-
# ensure chain(generator_expression) works.
784-
tasks = tasks[0]
785-
return reduce(operator.or_, tasks)
782+
if len(tasks) != 1 or is_list(tasks[0]):
783+
tasks = tasks[0] if len(tasks) == 1 else tasks
784+
return reduce(operator.or_, tasks)
786785
return super(chain, cls).__new__(cls, *tasks, **kwargs)
787786

788787

t/integration/test_canvas.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def test_simple_chain(self, manager):
2424
c = add.s(4, 4) | add.s(8) | add.s(16)
2525
assert c().get(timeout=TIMEOUT) == 32
2626

27+
@flaky
28+
def test_single_chain(self, manager):
29+
c = chain(add.s(3, 4))()
30+
assert c.get(timeout=TIMEOUT) == 7
31+
2732
@flaky
2833
def test_complex_chain(self, manager):
2934
c = (

t/unit/tasks/test_canvas.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ def test_apply(self):
441441
assert res.parent.parent.get() == 8
442442
assert res.parent.parent.parent is None
443443

444+
def test_single_expresion(self):
445+
x = chain(self.add.s(1, 2)).apply()
446+
assert x.get() == 3
447+
assert x.parent is None
448+
444449
def test_empty_chain_returns_none(self):
445450
assert chain(app=self.app)() is None
446451
assert chain(app=self.app).apply_async() is None

0 commit comments

Comments
 (0)