10000 ENH: Add support for argstr formatting of any iterable · nipype/pydra@1985e96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1985e96

Browse files
committed
ENH: Add support for argstr formatting of any iterable
1 parent d527133 commit 1985e96

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

pydra/engine/task.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ def _command_pos_args(self, field):
458458
cmd_add.append(argstr)
459459
else:
460460
sep = field.metadata.get("sep", " ")
461-
if argstr.endswith("...") and isinstance(value, list):
461+
if (
462+
argstr.endswith("...")
463+
and isinstance(value, ty.Iterable)
464+
and not isinstance(value, (str, bytes))
465+
):
462466
argstr = argstr.replace("...", "")
463467
# if argstr has a more complex form, with "{input_field}"
464468
if "{" in argstr and "}" in argstr:
@@ -474,7 +478,9 @@ def _command_pos_args(self, field):
474478
else:
475479
# in case there are ... when input is not a list
476480
argstr = argstr.replace("...", "")
477-
if isinstance(value, list):
481+
if isinstance(value, ty.Iterable) and not isinstance(
482+
value, (str, bytes)
483+
):
478484
cmd_el_str = sep.join([str(val) for val in value])
479485
value = cmd_el_str
480486
# if argstr has a more complex form, with "{input_field}"

pydra/engine/tests/test_shelltask.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,40 @@ def template_function(inputs):
16741674
assert shelly.output_dir == res.output.file_copy.parent
16751675

16761676

1677+
def test_shell_cmd_inputspec_with_iterable():
1678+
"""Test formatting of argstr with different iterable types."""
1679+
1680+
input_spec = SpecInfo(
1681+
name="Input",
1682+
fields=[
1683+
(
1684+
"iterable_1",
1685+
ty.Iterable[int],
1686+
{
1687+
"help_string": "iterable input 1",
1688+
"argstr": "--in1",
1689+
},
1690+
),
1691+
(
1692+
"iterable_2",
1693+
ty.Iterable[str],
1694+
{
1695+
"help_string": "iterable input 2",
1696+
"argstr": "--in2...",
1697+
},
1698+
),
1699+
],
1700+
bases=(ShellSpec,),
1701+
)
1702+
1703+
task = ShellCommandTask(name="test", input_spec=input_spec, executable="test")
1704+
1705+
for iterable_type in (list, tuple, set):
1706+
task.inputs.iterable_1 = iterable_type(range(3))
1707+
task.inputs.iterable_2 = iterable_type(["bar", "foo"])
1708+
assert task.cmdline == "test --in1 0 1 2 --in2 bar --in2 foo"
1709+
1710+
16771711
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
16781712
def test_shell_cmd_inputspec_copyfile_1(plugin, results_function, tmpdir):
16791713
"""shelltask changes a file in place,

0 commit comments

Comments
 (0)
0