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
Apologies for the late reply here. msgspec.Struct types will never work with the builtin functools.cached_property decorator, since that relies on a __dict__ and structs have no __dict__ allocated. But we could provide our own equivalent utility. This is currently blocked by #199; once that's in there'd be two ways we could spell this:
Option 1
frommsgspecimportStructfrommsgspec.structsimportcached_propertyclassFoo(Struct):
bar: tuple[str]
_bar_inner: str# This version of cached_property requires:# - explicitly annotating a new private field to store the cached value (_bar_inner here)# - passing that field name to the decorator@cached_property("_bar_inner")defbar_inner(self) ->str:
returnself.bar[0]
Option 2
frommsgspecimportStructfrommsgspec.structsimportcached_propertyclassFoo(Struct):
bar: tuple[str]
# This version of cached_property automatically allocates# the slot and marks it as private, no extra configuration needed@cached_propertydefbar_inner(self) ->str:
returnself.bar[0]
Option 1 is nice because once #199 is resolved, that version can be implemented in pure Python. The downside is it's more verbose and error prone to use.
Option 2 is nice because its usage is identical to that of functools.cached_property, except it would work on struct types. The downside is that it would require integration with the Struct metaclass to work properly. More work, but a slightly nicer API.
I'm not sure how often a cached_property decorator would be used with a msgspec.Struct type. I've never really needed a feature like this, but my needs aren't reflective of everyone. If the need is extremely rare, then I'd lean towards the first one as it's easier to do. If the need is less rare, then it may be worth doing some upfront work here in msgspec to make user's code simpler. Need to find the right balance.
Activity
jcrist commentedon Jan 26, 2023
Apologies for the late reply here.
msgspec.Structtypes will never work with the builtinfunctools.cached_propertydecorator, since that relies on a__dict__and structs have no__dict__allocated. But we could provide our own equivalent utility. This is currently blocked by #199; once that's in there'd be two ways we could spell this:Option 1
Option 2
Option 1 is nice because once #199 is resolved, that version can be implemented in pure Python. The downside is it's more verbose and error prone to use.
Option 2 is nice because its usage is identical to that of
functools.cached_property, except it would work on struct types. The downside is that it would require integration with theStructmetaclass to work properly. More work, but a slightly nicer API.I'm not sure how often a
cached_propertydecorator would be used with amsgspec.Structtype. I've never really needed a feature like this, but my needs aren't reflective of everyone. If the need is extremely rare, then I'd lean towards the first one as it's easier to do. If the need is less rare, then it may be worth doing some upfront work here inmsgspecto make user's code simpler. Need to find the right balance.jcrist commentedon Feb 17, 2023
Actually, thinking further I think we could just add an option to re-enable
__dict__access on struct types. So you'd declare your type as:Since this is a more general issue I've opened #324 to track it.
jcrist commentedon Feb 24, 2023
This is now supported by setting
dict=Trueon your struct class definition: