Skip to content

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

@lukasK9999

Description

@lukasK9999

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions