rnag/dataclass-wizard

YAMLWizard skip_defaults

Kryan90 opened this issue · 2 comments

  • Dataclass Wizard version: 0.22.2
  • Python version: 3.10
  • Operating System: MacOS

Description

I am trying to figure out the "correct" way to use skip_defaults with the YAMLWizard mixin

What I Did

I have found two ways to get it working

  1. By subclassing YAMLWizard
class NoDefaultsWizard(YAMLWizard):

    def __init_subclass__(cls, key_transform=None):
        DumpMeta(skip_defaults=True).bind_to(cls)

@dataclass
class TestClass(NoDefaultsWizard):
    a: list[int] = field(default_factory=list)
    b: list[int] = field(default_factory=list)

c = TestClass(a=[1, 2])
print(c.to_yaml())
  1. Binding after the class definition
@dataclass
class TestClass(YAMLWizard):
    a: list[int] = field(default_factory=list)
    b: list[int] = field(default_factory=list)

DumpMeta(skip_defaults=True).bind_to(TestClass)
c = TestClass(a=[1, 2])
print(c.to_yaml())

Are either of these the recommended approach?

I looked at the options here but it seems these only work for JSONSerializable classes of which YAMLWizard is not one

If either of these are preferred, I'd be happy to submit a PR to add their usage to the docs for YAMLWizard.

Thanks for the great library!