Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Support subset of tagged union strategy

Aperta
#348 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start at the configure_tagged_union entry point and reproduce the reported Foo and Bar examples with cattrs 23.1.0.dev0. Trace how configured tagged unions are matched and add coverage for a subset such as A | B; done means Bar(A(1)) uses the tagged-union strategy without requiring every combination to be configured.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

  • cattrs version: 23.1.0.dev0
  • Python version: 3.11.1
  • Operating System: Manjaro KDE
Description

Subsets of tagged unions should also be able to use the configured strategy.

What I Did
import attrs
import cattrs
from cattrs.strategies import configure_tagged_union


@attrs.define
class A:
    x: int


@attrs.define
class B:
    x: str


@attrs.define
class C:
    x: list[float]


@attrs.define
class Foo:
    y: A | B | C


@attrs.define
class Bar:
    y: A | B


c = cattrs.Converter()
configure_tagged_union(A | B | C, converter=c)
c.unstructure(Foo(A(1)))  # {'y': {'x': 1, '_type': 'A'}}
c.unstructure(Bar(A(1)))  # {'y': {'x': 1}}

Because Bar uses only A | B, and I've only configured A | B | C, it doesn't use the strategy.
I think it should be possible to use the strategy for A | B | C when I only have A | B, a subset of it. If not by default, this should be a configurable option, like configure_tagged_union(A | B | C, converter=c, allow_subset=True).

Otherwise, I'd need to repeatedly define all possible combinations of A, B, and C, like:

configure_tagged_union(A | B, converter=c)
configure_tagged_union(A | C, converter=c)
configure_tagged_union(B | C, converter=c)
configure_tagged_union(A | B | C, converter=c)
Lingua principale
Python
Stelle
1.1k
Fork
159
Merge medio
12h 21m
PR unite (30g)
6

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-attrs/cattrs

Tutte le issue di python-attrs/cattrs

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.