msgspec
msgspec copied to clipboard
Decoding a `generic` class hierarchy
Question
Hi, thanks for the great library!
Assuming this class hierarchy
import msgspec
from msgspec import Struct
class SystemsStatus[T: msgspec.Struct](Struct, tag=True): ...
class SystemsStatusInitial[T: msgspec.Struct](SystemsStatus[T]):
nodes: list[T]
class SystemsRemoved[T: msgspec.Struct](SystemsStatus[T]):
ids: list[int]
class SystemsUpdated[T: msgspec.Struct](SystemsStatus[T]):
nodes: list[T]
def get_union[T: msgspec.Struct](t: type[T]) -> type[SystemsStatus[T]]:
return SystemsRemoved[t] | SystemsStatusInitial[t] | SystemsUpdated[t]
DECODER = msgspec.json.Decoder(get_union(SomeDTO))
Is this the prefered way to create a decoder or I can do something like this
DECODER = msgspec.json.Decoder(SystemsStatus[SomeDTO])
Which failed decoding one of the inherited classes data.