8000 RF: Regroup checks for shallower logic · nipype/pydra@2eb09b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2eb09b7

Browse files
committed
RF: Regroup checks for shallower logic
1 parent a4a31f7 commit 2eb09b7

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

pydra/engine/specs.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,53 +161,47 @@ 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+
raise AttributeError(message)
199186

200187
# Raise error if multiple alternatives are set.
201-
if alternative_fields and any(alternative_fields.values()):
188+
elif alternatives_are_set:
202189
set_alternative_fields = [
203190
name for name, is_set in alternative_fields.items() if is_set
204191
]
205192
raise AttributeError(
206193
f"{field.name} is mutually exclusive with {set_alternative_fields}"
207194
)
208195

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

0 commit comments

Comments
 (0)
0