Skip to content

Commit 96d8d0f

Browse files
committed
Add check for 'xor' concurrently with 'mandatory'
1 parent e412f10 commit 96d8d0f

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

pydra/engine/specs.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,15 @@ def check_fields_input_spec(self):
162162
mdata = fld.metadata
163163
# checking if the mandatory field is provided
164164
if getattr(self, fld.name) is attr.NOTHING:
165-
if mdata.get("mandatory"):
166-
raise AttributeError(
167-
f"{fld.name} is mandatory, but no value provided"
168-
)
165+
if mdata.get("mandatory"):
166+
# checking if the mandatory field is provided elsewhere in the xor list
167+
alreday_populated = [getattr(self, el) for el in mdata["xor"] if (getattr(self, el) is not attr.NOTHING)]
168+
if alreday_populated: #another input satisfies mandatory attribute via xor condition
169+
continue
170+
else:
171+
raise AttributeError(
172+
f"{fld.name} is mandatory, but no value provided"
173+
)
169174
else:
170175
continue
171176
names.append(fld.name)

pydra/engine/tests/test_specs.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class SimpleTask(ShellCommandTask):
380380
{
381381
"help_string": "help",
382382
"mandatory": True,
383-
"xor": ("input_1", "input_2"),
383+
"xor": ("input_1", "input_2", "input_3"),
384384
}
385385
),
386386
(
@@ -390,9 +390,18 @@ class SimpleTask(ShellCommandTask):
390390
"help_string": "help",
391391
"mandatory": True,
392392
"argstr": "--i2",
393-
"xor": ("input_1", "input_2"),
393+
"xor": ("input_1", "input_2", "input_3"),
394394
}
395-
)
395+
),
396+
(
397+
"input_3",
398+
bool,
399+
{
400+
"help_string": "help",
401+
"mandatory": True,
402+
"xor": ("input_1", "input_2", "input_3"),
403+
}
404+
)
396405
]
397406
task_input_spec = SpecInfo(name="Input", fields=input_fields, bases=(ShellSpec,))
398407
task_output_fields = []
@@ -410,13 +419,24 @@ def test_task_inputs_mandatory_with_xOR_one_mandatory_is_OK():
410419
task.inputs.input_2 = attr.NOTHING
411420
task.inputs.check_fields_input_spec()
412421

413-
def test_task_inputs_mandatory_with_xOR_zero_mandatory_raises_error():
422+
def test_task_inputs_mandatory_with_xOR_one_mandatory_out_3_is_OK():
414423
"""input spec with mandatory inputs"""
415424
task = SimpleTask()
416425
task.inputs.input_1 = attr.NOTHING
417426
task.inputs.input_2 = attr.NOTHING
427+
task.inputs.input_3 = True
418428
task.inputs.check_fields_input_spec()
419429

430+
def test_task_inputs_mandatory_with_xOR_zero_mandatory_raises_error():
431+
"""input spec with mandatory inputs"""
432+
task = SimpleTask()
433+
task.inputs.input_1 = attr.NOTHING
434+
task.inputs.input_2 = attr.NOTHING
435+
with pytest.raises(Exception) as excinfo:
436+
task.inputs.check_fields_input_spec()
437+
assert "input_1 is mandatory, but no value provided" in str(excinfo.value)
438+
assert excinfo.type is AttributeError
439+
420440

421441
def test_task_inputs_mandatory_with_xOR_two_mandatories_raises_error():
422442
"""input spec with mandatory inputs"""
@@ -426,5 +446,17 @@ def test_task_inputs_mandatory_with_xOR_two_mandatories_raises_error():
426446

427447
with pytest.raises(Exception) as excinfo:
428448
task.inputs.check_fields_input_spec()
429-
assert "input_2 is mutually exclusive with ('input_1', 'input_2')" in str(excinfo.value)
449+
assert "input_2 is mutually exclusive with ('input_1', 'input_2'" in str(excinfo.value)
450+
assert excinfo.type is AttributeError
451+
452+
def test_task_inputs_mandatory_with_xOR_3_mandatories_raises_error():
453+
"""input spec with mandatory inputs"""
454+
task = SimpleTask()
455+
task.inputs.input_1 = 'Input1'
456+
task.inputs.input_2 = True
457+
task.inputs.input_3 = False
458+
459+
with pytest.raises(Exception) as excinfo:
460+
task.inputs.check_fields_input_spec()
461+
assert "input_2 is mutually exclusive with ('input_1', 'input_2', 'input_3'" in str(excinfo.value)
430462
assert excinfo.type is AttributeError

0 commit comments

Comments
 (0)