jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.44k stars 75 forks source link

How to use dict in msgspec.Struct #738

Closed harryle95 closed 2 months ago

harryle95 commented 2 months ago

Question

From the documentation:

dict (bool, default False) – Whether instances of this type will include a dict. Setting this to True will allow adding additional undeclared attributes to a struct instance, which may be useful for holding private runtime state. Defaults to False.

I am still abit confused as to how this works. Does it mean to say that at decoding, let's say from json bytes, any field that is not in __struct_fields__ will be put under __dict__? Or does it only mean that classes created under Struct(dict=True) will have an additional __dict__ attribute?

I believe it might be the second scenario at this point, but I am still asking this just in case I misunderstand anything.

Thanks

floxay commented 2 months ago

It's the second, https://github.com/jcrist/msgspec/pull/331 is the PR that added it and has additional info.