cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

Support tagged unions when the discriminator field is in a generic wrapper, not in the union type

Open lukasK9999 opened this issue 5 months ago • 1 comments

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?

lukasK9999 avatar May 26 '25 13:05 lukasK9999