Skip to content

Commit 677d3dd

Browse files
authored
Merge pull request #261 from djarecka/fix/result_none
[fix] allowing annotated function to return none (closes #256)
2 parents 2c3cad7 + 54b2ad4 commit 677d3dd

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

pydra/engine/task.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,21 @@ def _run_task(self):
199199
del inputs["_func"]
200200
self.output_ = None
201201
output = cp.loads(self.inputs._func)(**inputs)
202-
if output is not None:
203-
output_names = [el[0] for el in self.output_spec.fields]
204-
self.output_ = {}
205-
if len(output_names) > 1:
206-
if len(output_names) == len(output):
202+
output_names = [el[0] for el in self.output_spec.fields]
203+
if output is None:
204+
self.output_ = dict((nm, None) for nm in output_names)
205+
else:
206+
if len(output_names) == 1:
207+
# if only one element in the fields, everything should be returned together
208+
self.output_ = {output_names[0]: output}
209+
else:
210+
if isinstance(output, tuple) and len(output_names) == len(output):
207211
self.output_ = dict(zip(output_names, output))
208212
else:
209213
raise Exception(
210214
f"expected {len(self.output_spec.fields)} elements, "
211-
f"but {len(output)} were returned"
215+
f"but {output} were returned"
212216
)
213-
else: # if only one element in the fields, everything should be returned together
214-
self.output_[output_names[0]] = output
215217

216218

217219
class ShellCommandTask(TaskBase):

pydra/engine/tests/test_task.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,31 @@ def raise_exception(c, d):
309309
assert pytest.raises(Exception, bad_funk)
310310

311311

312+
def test_result_none_1():
313+
""" checking if None is properly returned as the result"""
314+
315+
@mark.task
316+
def fun_none(x):
317+
return None
318+
319+
task = fun_none(name="none", x=3)
320+
res = task()
321+
assert res.output.out is None
322+
323+
324+
def test_result_none_2():
325+
""" checking if None is properly set for all outputs """
326+
327+
@mark.task
328+
def fun_none(x) -> (ty.Any, ty.Any):
329+
return None
330+
331+
task = fun_none(name="none", x=3)
332+
res = task()
333+
assert res.output.out1 is None
334+
assert res.output.out2 is None
335+
336+
312337
def test_audit_prov(tmpdir):
313338
@mark.task
314339
def testfunc(a: int, b: float = 0.1) -> ty.NamedTuple("Output", [("out", float)]):

0 commit comments

Comments
 (0)