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

forbid_unknown_fields for dataclasses #791

Open
eamartin opened this issue Jan 2, 2025 · 1 comment
Open

forbid_unknown_fields for dataclasses #791

eamartin opened this issue Jan 2, 2025 · 1 comment

Comments

@eamartin
Copy link

eamartin commented Jan 2, 2025

It would be useful to be able to apply forbid_unknown_fields to dataclass deserialization and not just Structs.

Why:
I have a codebase where configs get deserialized into dataclasses.
forbid_unknown_fields=True is very useful for config deserialization as it catches typos and invalid config.
I'd prefer not to migrate all my dataclasses to Structs because:

  1. I have some code which uses methods from the dataclasses module: is_dataclass, fields, field. I'd rather not port this or have to maintain 2 copies (one which works for dataclasses and one for Structs).
  2. The codebase currently has some dataclasses which are never serialized. I don't know if I'd want to convert those to Structs as well.
  3. dataclasses are very widely used and for configs we're ok using something that isn't extremely fast.

How might the feature look:
Idea 1) decode and convert take a bool forbid_unknown_fields
Idea 2) all sub-objects beneath a Struct with forbid_unknown_fields=True will also have forbid_unknown_fields. I don't like this idea as much because it would be confusing for composition to change deserialization rules.

Questions:
Is there a way to hack this together that would involve wrapping just the decode/convert callsite and not updating every dataclass? I spent a little time looking at the code and couldn't find one.

Thanks a ton for your work on the lib! The support for generics is very nice

@eamartin
Copy link
Author

eamartin commented Jan 7, 2025

I put together a PR which adds a forbid_unknown_fields arg on all Decoder() classes and decode() methods.
#796

Behavior:

  • when flag is set to True, forbid unknown fields on ALL dataclasses, Structs, and TypedDicts. This includes Structs defined with forbid_unknown_fields=False.
  • when flag is False (the default), the current behavior stays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant