@@ -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 ]
0 commit comments