Raise more descriptive error on failure to parse data classes.

This commit is contained in:
Dain Nilsson 2022-08-24 09:18:28 +02:00
parent ae048d06ff
commit 103df1b456
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
1 changed files with 11 additions and 1 deletions

View File

@ -212,7 +212,12 @@ class _DataClassMapping(Mapping[_T, Any]):
value = getattr(self, f.name)
if value is None:
continue
value = _parse_value(hints[f.name], value)
try:
value = _parse_value(hints[f.name], value)
except (TypeError, KeyError, ValueError):
raise ValueError(
f"Error parsing field {f.name} for {self.__class__.__name__}"
)
object.__setattr__(self, f.name, value)
@classmethod
@ -252,6 +257,11 @@ class _DataClassMapping(Mapping[_T, Any]):
return None
if isinstance(data, cls):
return data
if not isinstance(data, Mapping):
raise TypeError(
f"{cls.__name__}.from_dict called with non-Mapping data of type"
f"{type(data)}"
)
kwargs = {}
for f in fields(cls):