subclass of `Box` does not unpickle correctly
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
Research direction
Run the reproduction program from the issue, then inspect box/box.py around line 236 where nested boxes are constructed during unpickling. Confirm that a Box subclass remains the type of nested values after pickle.dumps and pickle.loads; done means the reported output shows MyBox for both the root and key a.
Written by the indexing model from the issue text.
Description
Steps to reproduce
Execute this program:
import box
import pickle
class MyBox(box.Box):
...
b_in = MyBox()
b_in.a = dict()
print("in root: %s" % type(b_in))
print("in .a: %s" % type(b_in.a))
p = pickle.dumps(b_in, protocol=pickle.HIGHEST_PROTOCOL)
b_out = pickle.loads(p)
print("out root: %s" % type(b_out))
print("out .a: %s" % type(b_out.a))
Expected behavior
Output:
in root: <class '__main__.MyBox'>
in .a: <class '__main__.MyBox'>
out root: <class '__main__.MyBox'>
out .a: <class '__main__.MyBox'>
Actual behavior
Output:
in root: <class '__main__.MyBox'>
in .a: <class '__main__.MyBox'>
out root: <class '__main__.MyBox'>
out .a: <class 'box.box.Box'>
i.e., the type of key a is box.box.Box when it should be __main__.MyBox.
Comments
-
I believe the problem lies in this line:
https://github.com/cdgriffith/Box/blob/a4c10e977b574114613431394b30412b50aaacce/box/box.py#L236
where
Boxshould insted becls. -
Instantiating
b_inwithbox_class=MyBoxdoes not help, I assume because there is something about class instantiation I don’t understand. However it did seem like it should work. -
This does, however, work around the bug:
class MyBox(box.Box): def __new__(cls, *args, **kwargs): kwargs["box_class"] = MyBox return super().__new__(cls, *args, **kwargs)
- Dominant language
- Python
- Stars
- 2.8k
- Forks
- 135
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cdgriffith/Box
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
cdgriffith/Box#307 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
cdgriffith/Box#305 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 30/100
cdgriffith/Box#300 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
cdgriffith/Box#298 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
cdgriffith/Box#296 · 1 reaction ·
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100