-
Notifications
You must be signed in to change notification settings - Fork 83
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
omit_defaults
does not omit tuples and frozensets
#652
Comments
Thanks for opening this - this should be fixed by #653. Note that for these cases specifying the default value by value (rather than via import msgspec
class Backpack(msgspec.Struct, omit_defaults=True):
a_list: list[str] = []
a_tuple: tuple[str, ...] = ()
a_frozenset: frozenset[str] = frozenset()
instance = Backpack()
print(msgspec.json.encode(instance))
#> b'{}' The reason |
Thank you for the very swift fix! Especially useful with the tip on how to achieve this without waiting for a new release :-) |
Thanks to tips in jcrist/msgspec#652.
Thanks to tips in jcrist/msgspec#652.
Thanks to tips in jcrist/msgspec#652.
Thanks to tips in jcrist/msgspec#652.
Thanks to tips in jcrist/msgspec#652.
Description
When a struct has
omit_defaults=True
set, any fields defaulting todict
,list
, orset
is omitted from the serialization, as described in the docs.However, if one is using
tuple
instead oflist
andfrozenset
instead ofset
as default values, to make the struct as immutable as possible, those defaults are included in the encoded format.I think that
tuple
andfrozenset
should be handled in the same way asdict
,list, and
setby
omit_defaultsand
repr_omit_defaults`.Example
Output:
Expected:
The text was updated successfully, but these errors were encountered: