Skip to content

Commit 32432c3

Browse files
effigiesghisvail
authored andcommitted
REF: Further refactoring following code review
1 parent cb1304b commit 32432c3

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

pydra/engine/specs.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,53 +161,48 @@ def check_fields_input_spec(self):
161161
field_is_mandatory = bool(field.metadata.get("mandatory"))
162162
field_is_unset = getattr(self, field.name) is attr.NOTHING
163163

164+
if field_is_unset and not field_is_mandatory:
165+
continue
166+
164167
# Collect alternative fields associated with this field.
165168
alternative_fields = {
166169
name: getattr(self, name) is not attr.NOTHING
167170
for name in field.metadata.get("xor", [])
168171
if name != field.name
169172
}
173+
alternatives_are_set = any(alternative_fields.values())
170174

171-
# Collect required fields associated with this field.
172-
required_fields = {
173-
name: getattr(self, name) is not attr.NOTHING
174-
for name in field.metadata.get("requires", [])
175-
if name != field.name
176-
}
177-
178-
# Raise error if field is mandatory and unset
179-
# or no suitable alternative is provided.
175+
# Raise error if no field in mandatory alternative group is set.
180176
if field_is_unset:
181-
if field_is_mandatory:
182-
if alternative_fields:
183-
if any(alternative_fields.values()):
184-
# Alternative fields found, skip other checks.
185-
continue
186-
else:
187-
raise AttributeError(
188-
f"{field.name} is mandatory and unset, "
189-
"but no value provided by "
190-
f"{list(alternative_fields.keys())}."
191-
)
192-
else:
193-
raise AttributeError(
194-
f"{field.name} is mandatory, but no value provided."
195-
)
196-
else:
197-
# Field is not set, check the next one.
177+
if alternatives_are_set:
198178
continue
179+
message = f"{field.name} is mandatory and unset."
180+
if alternative_fields:
181+
raise AttributeError(
182+
message[:-1]
183+
+ f", but no alternative provided by {list(alternative_fields)}."
184+
)
185+
else:
186+
raise AttributeError(message)
199187

200188
# Raise error if multiple alternatives are set.
201-
if alternative_fields and any(alternative_fields.values()):
189+
elif alternatives_are_set:
202190
set_alternative_fields = [
203191
name for name, is_set in alternative_fields.items() if is_set
204192
]
205193
raise AttributeError(
206194
f"{field.name} is mutually exclusive with {set_alternative_fields}"
207195
)
208196

197+
# Collect required fields associated with this field.
198+
required_fields = {
199+
name: getattr(self, name) is not attr.NOTHING
200+
for name in field.metadata.get("requires", [])
201+
if name != field.name
202+
}
203+
209204
# Raise error if any required field is unset.
210-
if required_fields and not all(required_fields.values()):
205+
if not all(required_fields.values()):
211206
unset_required_fields = [
212207
name for name, is_set in required_fields.items() if not is_set
213208
]

pydra/engine/tests/test_shelltask_inputspec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,7 @@ def test_task_inputs_mandatory_with_xOR_zero_mandatory_raises_error():
22052205
with pytest.raises(Exception) as excinfo:
22062206
task.inputs.check_fields_input_spec()
22072207
assert "input_1 is mandatory" in str(excinfo.value)
2208-
assert "no value provided by ['input_2', 'input_3']" in str(excinfo.value)
2208+
assert "no alternative provided by ['input_2', 'input_3']" in str(excinfo.value)
22092209
assert excinfo.type is AttributeError
22102210

22112211

0 commit comments

Comments
 (0)