Skip to content

Commit c362ea1

Browse files
committed
fixed up handling of empty return values
1 parent 2b0a47d commit c362ea1

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

pydra/compose/base/helpers.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,10 @@ def extract_function_inputs_and_outputs(
253253
elif output is ty.Any:
254254
output = return_type
255255
outputs = {output_name: output}
256+
elif outputs == [] or return_type in (None, type(None)):
257+
outputs = {}
256258
else:
257-
if return_type not in (None, type(None)):
258-
outputs = {"out": return_type}
259-
else:
260-
outputs = {}
259+
outputs = {"out": return_type}
261260
return inputs, outputs
262261

263262

pydra/compose/python.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ def _run(self, job: "Job[PythonTask]", rerun: bool = True) -> None:
239239
return_names = [f.name for f in task_fields(self.Outputs)]
240240
if returned is None:
241241
job.return_values = {nm: None for nm in return_names}
242+
elif not return_names:
243+
raise ValueError(
244+
f"No output fields were specified, but the function returned {returned}"
245+
)
242246
elif len(return_names) == 1:
243247
# if only one element in the fields, everything should be returned together
244248
job.return_values[return_names[0]] = returned

pydra/compose/tests/test_python_fields.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,25 @@ def TestFunc(a: A):
427427

428428

429429
def test_no_outputs1():
430-
"""Test function tasks with object inputs"""
430+
"""Test function tasks with no outputs specified by None return type"""
431431

432432
@python.define
433433
def TestFunc(a: A) -> None:
434434
pass
435435

436436
outputs = TestFunc(a=A(x=7))()
437-
assert len(outputs) == 0
437+
assert not task_fields(outputs)
438438

439439

440440
def test_no_outputs2():
441-
"""Test function tasks with object inputs"""
441+
"""Test function tasks with no outputs set explicitly"""
442442

443443
@python.define(outputs=[])
444444
def TestFunc(a: A):
445445
pass
446446

447447
outputs = TestFunc(a=A(x=7))()
448-
assert len(outputs) == 0
448+
assert not task_fields(outputs)
449449

450450

451451
def test_no_outputs_fail():
@@ -455,7 +455,5 @@ def test_no_outputs_fail():
455455
def TestFunc(a: A):
456456
return a
457457

458-
with pytest.raises(
459-
ValueError, match="Returns an output but no outputs are defined"
460-
):
458+
with pytest.raises(ValueError, match="No output fields were specified"):
461459
TestFunc(a=A(x=7))()

0 commit comments

Comments
 (0)