Conversation
|
Hi @mvdbeek, thanks for the description and proposed solution. I'm not sure moving the instance variable to the class definition is the correct one. It works and doesn't do anything untoward, but it looks like a bug due to class vs instance variable confusion. I tried playing around with
So what I tried locally is replacing the def __new__(cls, *args, **kwds):
tracked = super().__new__(cls, *args, **kwds)
tracked.parent = None
return trackedI've tried that locally with a few simple tests and everything seems to behave as it should, but if you could give it a try on your more real-world test case, I'd like to hear whether it works for you. For completeness I've included the test script: import pickle
from sqlalchemy_json import NestedMutableDict
one = NestedMutableDict({"numbers": [1, 2, 3, 4]})
two = NestedMutableDict({"numbers": [5, 6, 7]})
one_reloaded = pickle.loads(pickle.dumps(one))
two_reloaded = pickle.loads(pickle.dumps(two))
assert one == one_reloaded
assert two == two_reloaded
one_reloaded["numbers"].append(5)
assert one_reloaded["numbers"] == [1, 2, 3, 4, 5]
assert one["numbers"] == [1, 2, 3, 4]
assert one_reloaded["numbers"].parent is one_reloaded
assert two_reloaded["numbers"].parent is two_reloaded
assert one_reloaded is not two_reloaded
print("Pickle -> unpickle works") |
|
excellent, yes, that works too! |
Many thanks @edeloof, this looks much better than adding parent to the class variable.
42204aa to
4b0e171
Compare
|
I've added your proposed fix and added some unit tests going through the examples from the readme. |
|
Thanks a bunch, especially for the tests! I'll look into actually doing a new release to PyPI based on the recent changes when I find the time for it. |
We ran into the following traceback while unpickling
and object that contained a NestedMutableDict:
This works as a quick fix for us. The pickle behavior of not running
though
__init__is decribed hereI'm not sure if this is the right fix, any help is appreciated @edelooff.