You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the messages that msgspec raises on validation error can be a bit cryptic:
In [1]: importmsgspecIn [2]: classUser(msgspec.Struct):
...: name: str
...: email: str
...:
In [3]: dec=msgspec.Decoder(User)
In [4]: enc=msgspec.Encoder()
In [5]: msg=enc.encode({"name": 1})
In [6]: dec.decode(msg)
---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)
<ipython-input-6-e19f0160a9aa>in<module>---->1dec.decode(msg)
TypeError: Errordecoding: expected`str`, got`int`
The message isn't incorrect, but it doesn't provide some necessary context for determining what part of the message was mistyped. For highly nested messages I'm not sure what this would look like. Right now I'm leaning towards something like:
"Error while decoding {context}: {details}"
The idea is to use either the most recent struct field in the type tree before the error, or the top-level compound type as context, then let the error to the right of the : speak for itself.
>>>Decoder(User).decode(...) # For structs, include struct name, current field, and type of that fieldDecodeError("Error while decoding `User.name` (`str`): expected `str`, got `int`")
>>>Decoder(List[User]).decode(...) # For collections, include collection typeDecodeError("Error while decoding `List[User]`: expected `User`, got `int`")
>>>Decoder(Dict[Tuple[str, str]], User]).decode(...) # For dicts, specify if in `key` or `value`DecodeError("Error while decoding `key` of `Dict[Tuple[str, str], str]`: expected `tuple` got `str`")
>>>Decoder(List[User]).decode(...) # if error is inside a struct, even if top-level is a collection, use struct as contextDecodeError("Error while decoding `User.name` (`str`): expected `str`, got `int`")
The hope is that this will be easy enough to implement in a performant way, while still providing decent error messages for debugging.
Activity
jcrist commentedon Feb 22, 2021
This has now been resolved since 998c657.
Fix `Meta(pattern=..)` memory leak / corruption (msgspec#1256)