Make the exception show you all unknown keys at once

In case a response type contains an unknown key, there are probably some 
more.
This commit is contained in:
clerie 2022-06-16 23:00:22 +02:00
parent 501be198c4
commit ac9ae74a0b

View File

@ -39,12 +39,16 @@ class ChildType:
return "{}({})".format(self.__class__.__name__, repr(self._attrs)) return "{}({})".format(self.__class__.__name__, repr(self._attrs))
def cast_dict_to_childtype(t, d): def cast_dict_to_childtype(t, d):
errors = {} # collect unknown keys
for k, v in d.items(): for k, v in d.items():
if k in t.FIELDS.keys(): if k in t.FIELDS.keys():
if t.FIELDS[k] is not None and type(v) != t.FIELDS[k]: if t.FIELDS[k] is not None and type(v) != t.FIELDS[k]:
d[k] = t.FIELDS[k](v) d[k] = t.FIELDS[k](v)
else: else:
raise KeyError("Key '{}' containing '{}' is unknown for {}".format(k, v, t.__name__)) errors[k] = v
if errors != {}:
raise KeyError("The following keys are unknown for '{}': {}".format(t.__name__, errors))
return t(d) return t(d)