Skip to content

AeromancyStruct.validate needs better error messages #31

@dmcc

Description

@dmcc
[...]
src/colocation/colocation.py:29: in __init__
    self.metadata.validate()
.venv/lib/python3.10/site-packages/aeromancy/struct.py:87: in validate
    self.decode(self.encode(format="msgpack"), format="msgpack")
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'colocation.structs.Metadata'>
encoded_bytes = b'\x83\xabinstruments\x9d\x91\x8d\xadinstrument_id\xa8ref-PM25\xafinstrument_type\x01\xa9pollutant\xa4pm25\xaftime_res...etary\xa4note\xc0\xa5parts\xc0\xa9num_parts\x01\xafgps_coordinates\x92\xcb@P6\x1f\xc8\xf3#y\xcb\xc0bwF\xf7\xa0\xb5\xee'
format = 'msgpack'

    @classmethod
    def decode(cls, encoded_bytes: bytes, format: str):
        """Deserialize this `msgspec.Struct` from bytes.
    
        Parameters
        ----------
        encoded_bytes
            Bytes to attempt to deserialize.
        format
            The format to used to encode the `msgspec.Struct`. This can be
            "yaml", "json", or "msgpack".
    
        Returns
        -------
        An instance of this class decoded according to `format`.
        """
        match format:
            case "yaml":
                return msgspec.yaml.decode(encoded_bytes, type=cls)
            case "json":
                return msgspec.json.decode(encoded_bytes, type=cls)
            case "msgpack":
>               return msgspec.msgpack.decode(encoded_bytes, type=cls)
E               msgspec.ValidationError: Expected `object`, got `array` - at `$.instruments[0]`

.venv/lib/python3.10/site-packages/aeromancy/struct.py:71: ValidationError

Catching and rethrowing the message with more details should help substantially.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions