Skip to content

subclass of Box does not unpickle correctly #308

Description

@reidpr

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

  1. I believe the problem lies in this line:

    Box/box/box.py

    Line 236 in a4c10e9

    "box_class": box_class if box_class is not None else Box,

    where Box should insted be cls.

  2. Instantiating b_in with box_class=MyBox does not help, I assume because there is something about class instantiation I don’t understand. However it did seem like it should work.

  3. 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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions