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
Add option to add __dict__ on msgspec.Struct types #324
Currently msgspec.Struct types are __slots__ only, defining no instance dicts. This is for both efficiency (slots reduce memory usage and require no dict-lookup for internal access) and correctness (no instance __dict__ means you get a runtime error for typos when setting attributes). However sometimes it can be useful to have a lazily-created __dict__ defined. One use case is functools.cached_property support, which only works for objects with a __dict__ defined (#204).
We should enable this as an opt-in feature. Something like:
classPoint(Struct, dict=True): # probably a better option namex: inty: int# both `x` and `y` are still stored in `__slots__`, no `__dict__` allocated yetp=Point(1, 2)
# Setting an undeclared attribute will allocate a `__dict__` and store the attribute therep.z=3# undeclared attributes are ignored by msgspec's encodersmsgspec.json.encode(p)
# {"x":1,"y":2}
Activity