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
I'm using attrs with subclasses of persistent.Persistent, which allows for persisting python objects in ZODB (Zope DB http://www.zodb.org). This is a pretty popular library, which inherently would benefit from being able to persist somewhat immutable objects, think LineItems in an Order for a Shop.
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/attr/_make.py in _frozen_setattrs(self, name, value)
386 Attached to frozen classes as __setattr__.
387 """
--> 388 raise FrozenInstanceError()
389
390
FrozenInstanceError:
After some research, I was able to determine that for full functionality, ZODB persistent objects need the ability to write to attributes prefixed with _p_ representing persistent state in the db, and _v_ for volatile attributes that won't be persisted, used for instance with a caching decorator, etc.
With that information, I wrote a decorator that monkeypatches attr._make._frozen_setattrs and attr._make._frozen_delattrs to allow for modifying attributes beginning with _p_ and _v_.
So obviously this is pretty hacky and I would love if there was an officially supported, generic way of hooking into the frozen class's __setattr__ and __delattr__ methods.
Although I think a module level tuple of whitelisted/blacklisted prefixes would work well in this case, I was thinking something similar to this will likely be requested in the future again, to work with another package, so providing a more flexible hook might be best.
Perhaps the ability to provide a decorator, or replacement functions would provide as much functionality as anyone would need down the line. I can volunteer my time to help implement this, but would like to open this issue to see what other people think about all of this. Such an interface could be generic and allow for added hooks, configuration in the future.
The text was updated successfully, but these errors were encountered:
Hello,
I'm using attrs with subclasses of
persistent.Persistent
, which allows for persisting python objects in ZODB (Zope DB http://www.zodb.org). This is a pretty popular library, which inherently would benefit from being able to persist somewhat immutable objects, think LineItems in an Order for a Shop.Quick test, failing
Which currently results in the following:
After some research, I was able to determine that for full functionality, ZODB persistent objects need the ability to write to attributes prefixed with
_p_
representing persistent state in the db, and_v_
for volatile attributes that won't be persisted, used for instance with a caching decorator, etc.With that information, I wrote a decorator that monkeypatches
attr._make._frozen_setattrs
andattr._make._frozen_delattrs
to allow for modifying attributes beginning with_p_
and_v_
.The following test now works
So obviously this is pretty hacky and I would love if there was an officially supported, generic way of hooking into the frozen class's
__setattr__
and__delattr__
methods.Although I think a module level tuple of whitelisted/blacklisted prefixes would work well in this case, I was thinking something similar to this will likely be requested in the future again, to work with another package, so providing a more flexible hook might be best.
Perhaps the ability to provide a decorator, or replacement functions would provide as much functionality as anyone would need down the line. I can volunteer my time to help implement this, but would like to open this issue to see what other people think about all of this. Such an interface could be generic and allow for added hooks, configuration in the future.
The text was updated successfully, but these errors were encountered: