You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cover omit_defaults handling in asdict and to_builtins documentation #778
(I'm not sure if this is a bug report or a feature request, so I opted for a feature request in case the current behaviour is intentional)
I am using msgspec to help communicate with a server where a boolean field can be set to None in the data model not because null is a valid value, but because it is permitted to omit the key entirely. The relevant struct has omit_defaults=True set accordingly.
However, msgspec isn't handling the full message serialisation (httpx is doing that), so I attempted to use asdict to generate the message spec portion of the message.
This failed, as asdict includes all fields, without paying attention to omit_defaults.
changed the title [-]`asdict` should respect `omit_defaults`[/-][+]Feature: add alternative API to `asdict` that respects `omit_defaults`[/+]on Dec 5, 2024
changed the title [-]Feature: add alternative API to `asdict` that respects `omit_defaults`[/-][+]Cover `omit_defaults` handling in `asdict` and `to_builtins` documentation[/+]on Dec 5, 2024
After exploring the docs further, I discovered that the "recursively convert to builtin types, while respecting omit_defaults" API exists: it is to_builtins.
Neither msgspec.struct.asdict nor msgspec.to_builtins mention omit_defaults in their documentation, so even though they have "See also" links to each other, it isn't clear why you might choose one over the other. (#748 is another example of someone hitting a similar point of confusion).
Activity
[-]`asdict` should respect `omit_defaults`[/-][+]Feature: add alternative API to `asdict` that respects `omit_defaults`[/+][-]Feature: add alternative API to `asdict` that respects `omit_defaults`[/-][+]Cover `omit_defaults` handling in `asdict` and `to_builtins` documentation[/+]ncoghlan commentedon Dec 5, 2024
After exploring the docs further, I discovered that the "recursively convert to builtin types, while respecting
omit_defaults" API exists: it isto_builtins.Neither
msgspec.struct.asdictnormsgspec.to_builtinsmentionomit_defaultsin their documentation, so even though they have "See also" links to each other, it isn't clear why you might choose one over the other. (#748 is another example of someone hitting a similar point of confusion).The other thing that tripped me up was that https://jcristharif.com/msgspec/usage.html doesn't cover the non-serialisation case, so the fact that
msgspec.to_builtinsis the encoding equivalent andmsgspec.convertis the decoding equivalent wasn't immediately apparent.I'll create a PR with some suggested docs tweaks.
Cover conversion to/from builtins in usage docs
ncoghlan commentedon Dec 5, 2024
#780 attempts to cover the points above in both the main usage docs, as well as in the docstrings of
to_builtinsandasdict.Document differences between structs.asdict/astuple and to_builtins (#…