python-attrs / python-attrs/cattrs
Support tagged unions when the discriminator field is in a generic wrapper, not in the union type
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
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.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100