I'm dealing with line-delimited JSON files in which one field can be of multiple types, which all have very different structures. So I naturally defined a base Struct to parse all of these, doing something like:
class BaseType(msgspec.Struct, tag_field="tag"):
pass
class InterestingSubType(BaseType):
id: int
...
However, I'm only interested in parsing a few of these types, so I end up having many
class NotInterestingSubType(BaseType):
pass
and in the very first parent class:
class ObjectToParse(msgpec.Struct):
...
field: InterestingSubType | NotInterestingSubType | ....
To avoid all this, I would have enjoyed being able to do just
class ObjectToParse(msgpec.Struct):
...
field: InterestingSubType | BaseType
to mean that if the interesting tagged type is found, parse it as I described it, otherwise just return an empty BaseType to signal there was one of the NotInterestingSubTypes there. Also, in the same way that you can forbid unknown fields, you could also forbid unknown tags to get the current behavior.
I hope I managed to convey my idea clearly. Also I must say I've used msgspec only a little until now so I'm not 100% sure that's not possible already, but I've looked and tested quite extensively.
Description
I'm dealing with line-delimited JSON files in which one field can be of multiple types, which all have very different structures. So I naturally defined a base
Struct
to parse all of these, doing something like:However, I'm only interested in parsing a few of these types, so I end up having many
and in the very first parent class:
To avoid all this, I would have enjoyed being able to do just
to mean that if the interesting tagged type is found, parse it as I described it, otherwise just return an empty
BaseType
to signal there was one of theNotInterestingSubType
s there. Also, in the same way that you can forbid unknown fields, you could also forbid unknown tags to get the current behavior.I hope I managed to convey my idea clearly. Also I must say I've used
msgspec
only a little until now so I'm not 100% sure that's not possible already, but I've looked and tested quite extensively.