msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

forbid_unknown_fields for dataclasses

Open eamartin opened this issue 11 months ago • 1 comments

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 avatar Jan 02 '25 23:01 eamartin

I put together a PR which adds a forbid_unknown_fields arg on all Decoder() classes and decode() methods. https://github.com/jcrist/msgspec/pull/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

eamartin avatar Jan 07 '25 22:01 eamartin