Closed
Description
Description
When a struct has omit_defaults=True
set, any fields defaulting to dict
, list
, or set
is omitted from the serialization, as described in the docs.
However, if one is using tuple
instead of list
and frozenset
instead of set
as default values, to make the struct as immutable as possible, those defaults are included in the encoded format.
I think that tuple
and frozenset
should be handled in the same way as dict
, list, and
setby
omit_defaultsand
repr_omit_defaults`.
Example
import msgspec
class Backpack(msgspec.Struct, omit_defaults=True):
a_list: list[str] = msgspec.field(default_factory=list)
a_tuple: tuple[str, ...] = msgspec.field(default_factory=tuple)
a_frozenset: frozenset[str] = msgspec.field(default_factory=frozenset)
instance = Backpack()
print(msgspec.json.encode(instance))
Output:
b'{"a_tuple":[],"a_frozenset":[]}'
Expected:
b'{}'
Metadata
Metadata
Assignees
Labels
No labels