Skip to content

Commit 306ac27

Browse files
authored
Merge pull request #565 from ghisvail/feat/run-task-dict
Add support for arbitrary mappings in a FunctionTask
2 parents 8967b58 + b89f9a2 commit 306ac27

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pydra/engine/task.py

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def _run_task(self):
198198
self.output_ = {output_names[0]: output}
199199
elif isinstance(output, tuple) and len(output_names) == len(output):
200200
self.output_ = dict(zip(output_names, output))
201+
elif isinstance(output, dict):
202+
self.output_ = {key: output.get(key, None) for key in output_names}
201203
else:
202204
raise RuntimeError(
203205
f"expected {len(self.output_spec.fields)} elements, "

pydra/engine/tests/test_task.py

+21
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ def testfunc(
114114
]
115115

116116

117+
def test_annotated_func_dictreturn(use_validator):
118+
"""Test mapping from returned dictionary to output spec."""
119+
120+
@mark.task
121+
@mark.annotate({"return": {"sum": int, "mul": int}})
122+
def testfunc(a: int, b: int):
123+
return dict(sum=a + b, diff=a - b)
124+
125+
task = testfunc(a=2, b=3)
126+
result = task()
127+
128+
# Part of the annotation and returned, should be exposed to output.
129+
assert result.output.sum == 5
130+
131+
# Part of the annotation but not returned, should be coalesced to None.
132+
assert result.output.mul is None
133+
134+
# Not part of the annotation, should be discarded.
135+
assert not hasattr(result.output, "diff")
136+
137+
117138
def test_annotated_func_multreturn(use_validator):
118139
"""the function has two elements in the return statement"""
119140

0 commit comments

Comments
 (0)