Skip to content

forbid_unknown_fields for dataclasses #791

Open
@eamartin

Description

@eamartin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions