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

Open
#651 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Start by reading configure_tagged_union, especially its union.args handling, and trace how the discriminator tag_name is resolved. Determine whether KafkaMessage[Data] | KafkaMessage[Heartbeat] can be configured with the wrapper-level type field, and verify conversion for both payload variants.

Written by the indexing model from the issue text.

Description

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?

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/cattrs

All issues in python-attrs/cattrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.