cattrs
cattrs copied to clipboard
Support tagged unions when the discriminator field is in a generic wrapper, not in the union type
Hello, I’m not sure if I just can’t figure out how to do it or if it’s not supported.
I have a generic message envelope like this:
@frozen
class KafkaMessage(Generic[T]):
type: str # discriminator
payload: T
And payload can be one of several types:
@frozen
class Data: ...
@frozen
class Heartbeat: ...
I’d like to define and structure:
type KafkaMessageUnion = KafkaMessage[Data] | KafkaMessage[Heartbeat]
configure_tagged_union(KafkaMessageUnion, CONVERTER, tag_name="type")
however this fails on very first line of configure_tagged_union - args = union.__args__
Is it possible to get around this somehow?