Skip to content

Add option to define __dict__ on Struct type - #331

Merged
jcrist merged 1 commit into
mainfrom
add-struct-dict-option
Feb 24, 2023
Merged

jcrist merged 1 commit into
mainfrom
add-struct-dict-option

Conversation

@jcrist

@jcrist jcrist commented Feb 24, 2023

Copy link
Copy Markdown
Member

This adds a new dict option on a Struct type, disabled by default. Enabling this will add a lazily-allocated __dict__ attribute to all instances of that type, which can be used to store additional runtime state. Note that any data stored in the __dict__ is not encoded/decoded/pickled/copied by default.

One use of this is to support functools.cached_property on Struct instances, since this decorator stores the cached value on the instance __dict__.

Fixes #324, fixes #204.

This adds a new `dict` option on a `Struct` type, disabled by default.
Enabling this will add a lazily-allocated `__dict__` attribute to all
instances of that type, which can be used to store additional runtime
state. Note that any data stored in the `__dict__` is _not_
encoded/decoded/pickled/copied by default.

One use of this is to support `functools.cached_property` on `Struct`
instances, since this decorator stores the cached value on the instance
`__dict__`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add option to add __dict__ on msgspec.Struct types functools.cached_property support?

1 participant