@@ -161,53 +161,48 @@ 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
8000
# 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
+ else :
186
+ raise AttributeError (message )
199
187
200
188
# Raise error if multiple alternatives are set.
201
- if alternative_fields and any ( alternative_fields . values ()) :
189
+ elif alternatives_are_set :
202
190
set_alternative_fields = [
203
191
name for name , is_set in alternative_fields .items () if is_set
204
192
]
205
193
raise AttributeError (
206
194
f"{ field .name } is mutually exclusive with { set_alternative_fields } "
207
195
)
208
196
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
+
209
204
# 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 ()):
211
206
unset_required_fields = [
212
207
name for name , is_set in required_fields .items () if not is_set
213
208
]
0 commit comments