@@ -161,53 +161,47 @@ def check_fields_input_spec(self):
161
161
field_is_mandatory = bool (field .metadata .get ("mandatory" ))
162
162
field_is_unset = getattr (self , field .name ) is attr .NOTHING
163
163
164
+ if field_is_unset and not field_is_mandatory :
165
+ continue
166
+
164
167
# Collect alternative fields associated with this field.
165
168
alternative_fields = {
166
169
name : getattr (self , name ) is not attr .NOTHING
167
170
for name in field .metadata .get ("xor" , [])
168
171
if name != field .name
169
172
}
173
+ alternatives_are_set = any (alternative_fields .values ())
170
174
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.
180
176
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 :
198
178
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 )
199
186
200
187
# Raise error if multiple alternatives are set.
201
- if alternative_fields and any ( alternative_fields . values ()) :
188
+ elif alternatives_are_set :
202
189
set_alternative_fields = [
203
190
name for name , is_set in alternative_fields .items () if is_set
204
191
]
205
192
raise AttributeError (
206
193
f"{ field .name } is mutually exclusive with { set_alternative_fields } "
207
194
)
208
195
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
+
209
203
# 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 ()):
211
205
unset_required_fields = [
212
206
name for name , is_set in required_fields .items () if not is_set
213
207
]
0 commit comments