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
Although not part of the official API, I find add_slots to be quite useful. However, the combination with frozen dataclasses and pickling causes problems:
dataclasses.FrozenInstanceError: cannot assign to field 'foo'
A quick fix would be to add something like this:
def_dataclass_getstate(self):
return [getattr(self, f.name) forfinfields(self)]
def_dataclass_setstate(self, state):
forfield, valueinzip(fields(self), state):
# use setattr because dataclass may be frozenobject.__setattr__(self, field.name, value)
defadd_slots(cls):
... # existing add_slots code here...# optionally only do these steps if the dataclass is frozencls.__getstate__=_dataclass_getstatecls.__setstate__=_dataclass_setstatereturncls
edit: typos
The text was updated successfully, but these errors were encountered:
Although not part of the official API, I find
add_slots
to be quite useful. However, the combination with frozen dataclasses and pickling causes problems:gives the following error:
A quick fix would be to add something like this:
edit: typos
The text was updated successfully, but these errors were encountered: