Skip to content

Commit c7518fb

Browse files
authored
Merge pull request #217 from djarecka/mnt/input_spec_repr
allowing for simpler syntax for input spec (closes #176)
2 parents 7ceb4de + 6d8d26e commit c7518fb

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

pydra/engine/helpers.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,29 @@ def make_klass(spec):
221221
if isinstance(item[1], attr._make._CountingAttr):
222222
newfields[item[0]] = item[1]
223223
else:
224-
newfields[item[0]] = attr.ib(type=item[1])
224+
newfields[item[0]] = attr.ib(repr=False, type=item[1])
225225
else:
226-
if isinstance(item[2], attr._make._CountingAttr):
227-
raise ValueError("Three part should not have attr")
228-
# newfields[item[0]] = item[2]
226+
if (
227+
any([isinstance(ii, attr._make._CountingAttr) for ii in item])
228+
or len(item) > 4
229+
):
230+
raise ValueError(
231+
"syntax not valid, you can use (name, attr), "
232+
"(name, type, default), (name, type, default, metadata)"
233+
"or (name, type, metadata)"
234+
)
229235
else:
230-
newfields[item[0]] = attr.ib(item[2], type=item[1])
236+
if len(item) == 3:
237+
name, tp = item[:2]
238+
if isinstance(item[-1], dict) and "help_string" in item[-1]:
239+
mdata = item[-1]
240+
newfields[name] = attr.ib(type=tp, metadata=mdata)
241+
else:
242+
dflt = item[-1]
243+
newfields[name] = attr.ib(type=tp, default=dflt)
244+
elif len(item) == 4:
245+
name, tp, dflt, mdata = item
246+
newfields[name] = attr.ib(type=tp, default=dflt, metadata=mdata)
231247
fields = newfields
232248
return attr.make_class(spec.name, fields, bases=spec.bases, kw_only=True)
233249

pydra/engine/tests/test_shelltask.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,32 @@ def test_shell_cmd_inputspec_3(plugin, results_function):
392392
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
393393
@pytest.mark.parametrize("plugin", Plugins)
394394
def test_shell_cmd_inputspec_3a(plugin, results_function):
395+
""" mandatory field added to fields, value provided
396+
using shorter syntax for input spec (no attr.ib)
397+
"""
398+
cmd_exec = "echo"
399+
hello = "HELLO"
400+
my_input_spec = SpecInfo(
401+
name="Input",
402+
fields=[
403+
("text", str, {"position": 1, "help_string": "text", "mandatory": True})
404+
],
405+
bases=(ShellSpec,),
406+
)
407+
408+
# separate command into exec + args
409+
shelly = ShellCommandTask(
410+
name="shelly", executable=cmd_exec, text=hello, input_spec=my_input_spec
411+
)
412+
assert shelly.inputs.executable == cmd_exec
413+
assert shelly.cmdline == "echo HELLO"
414+
res = results_function(shelly, plugin)
415+
assert res.output.stdout == "HELLO\n"
416+
417+
418+
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
419+
@pytest.mark.parametrize("plugin", Plugins)
420+
def test_shell_cmd_inputspec_3b(plugin, results_function):
395421
""" mandatory field added to fields, value provided after init"""
396422
cmd_exec = "echo"
397423
hello = "HELLO"
@@ -421,7 +447,7 @@ def test_shell_cmd_inputspec_3a(plugin, results_function):
421447

422448

423449
@pytest.mark.parametrize("plugin", Plugins)
424-
def test_shell_cmd_inputspec_3b_exception(plugin):
450+
def test_shell_cmd_inputspec_3c_exception(plugin):
425451
""" mandatory field added to fields, value is not provided, so exception is raised """
426452
cmd_exec = "echo"
427453
my_input_spec = SpecInfo(
@@ -511,6 +537,31 @@ def test_shell_cmd_inputspec_4(plugin, results_function):
511537
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
512538
@pytest.mark.parametrize("plugin", Plugins)
513539
def test_shell_cmd_inputspec_4a(plugin, results_function):
540+
""" mandatory field added to fields, value provided
541+
using shorter syntax for input spec (no attr.ib)
542+
"""
543+
cmd_exec = "echo"
544+
my_input_spec = SpecInfo(
545+
name="Input",
546+
fields=[("text", str, "Hello", {"position": 1, "help_string": "text"})],
547+
bases=(ShellSpec,),
548+
)
549+
550+
# separate command into exec + args
551+
shelly = ShellCommandTask(
552+
name="shelly", executable=cmd_exec, input_spec=my_input_spec
553+
)
554+
555+
assert shelly.inputs.executable == cmd_exec
556+
assert shelly.cmdline == "echo Hello"
557+
558+
res = results_function(shelly, plugin)
559+
assert res.output.stdout == "Hello\n"
560+
561+
562+
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
563+
@pytest.mark.parametrize("plugin", Plugins)
564+
def test_shell_cmd_inputspec_4b(plugin, results_function):
514565
""" mandatory field added to fields, value provided """
515566
cmd_exec = "echo"
516567
my_input_spec = SpecInfo(
@@ -1170,6 +1221,33 @@ def test_shell_cmd_inputspec_state_1(plugin, results_function):
11701221
assert res[1].output.stdout == "hi\n"
11711222

11721223

1224+
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
1225+
@pytest.mark.parametrize("plugin", Plugins)
1226+
def test_shell_cmd_inputspec_state_1a(plugin, results_function):
1227+
""" adding state to the input from input_spec
1228+
using shorter syntax for input_spec (without default)
1229+
"""
1230+
cmd_exec = "echo"
1231+
hello = ["HELLO", "hi"]
1232+
my_input_spec = SpecInfo(
1233+
name="Input",
1234+
fields=[
1235+
("text", str, {"position": 1, "help_string": "text", "mandatory": True})
1236+
],
1237+
bases=(ShellSpec,),
1238+
)
1239+
1240+
# separate command into exec + args
1241+
shelly = ShellCommandTask(
1242+
name="shelly", executable=cmd_exec, text=hello, input_spec=my_input_spec
1243+
).split("text")
1244+
assert shelly.inputs.executable == cmd_exec
1245+
1246+
res = results_function(shelly, plugin)
1247+
assert res[0].output.stdout == "HELLO\n"
1248+
assert res[1].output.stdout == "hi\n"
1249+
1250+
11731251
@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter])
11741252
@pytest.mark.parametrize("plugin", Plugins)
11751253
def test_shell_cmd_inputspec_state_2(plugin, results_function):

0 commit comments

Comments
 (0)