Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAMLWizard skip_defaults #86

Open
Kryan90 opened this issue May 10, 2023 · 2 comments
Open

YAMLWizard skip_defaults #86

Kryan90 opened this issue May 10, 2023 · 2 comments
Labels
acknowledged awaiting-user-input Awaiting user input / feedback. documentation Improvements or additions to documentation

Comments

@Kryan90
Copy link

Kryan90 commented May 10, 2023

  • 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!

@adamcunnington-mlg
Copy link

@Kryan90 I'm wondering if you can help as I have a similar question.

I am trying to set json_key_to_field which is a Meta argument but not a DumpMeta argument. I also can't use the Meta subclass because it's not used by the Yaml mixin. How can I do this - is there some similar way I can achieve it within the init_subclass method?

@rnag
Copy link
Owner

rnag commented Nov 4, 2024

@Kryan90 Apologies for the (long) delay. I am slowly getting back into checking on GitHub issues.

The First approach seems best to me. This is because it skips default __init_subclass__ which assigns key transform of LetterCase.LISP and binds it to the class. With this approach, it is clean, skips any default key transform, and only sets skip_defaults in the DumpMeta. I like this approach, though the Second one is a valid approach as well.

For sure, I would be happy to review a PR to add their usage to the docs for YAMLWizard.

@adamcunnington-mlg I would suggest looking into LoadMeta, which IIRC supports the json_key_to_field argument that you are asking about. Hope this helps!

@rnag rnag added documentation Improvements or additions to documentation acknowledged awaiting-user-input Awaiting user input / feedback. labels Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged awaiting-user-input Awaiting user input / feedback. documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants