-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enums #101
Comments
I believe the root of this issue is that the class dict holds instances of the type it. In [1]: class C:
...: pass
...:
In [2]: C.c = C()
In [3]: cp.dumps(C) |
That's correct. Standard In [64]: class C():
...: pass
...:
In [65]: C.c = C()
In [66]: import pickle
In [67]: pickle.dumps(C)
Out[67]: b'\x80\x03c__main__\nC\nq\x00.
In [77]: assert hasattr(pickle.loads(pickle.dumps(C)), 'c') |
@mrocklin @AndrewPashkin as of #102 trying to serialize an enum no longer triggers an infinite recursion, and it works for enums defined as non- One worry I'd have about supporting dynamic enums is that it's idiomatic to use |
I personally have no practical experience with Enum types. I was just pushing a Dask error upstream here. |
Indeed:
Would |
@AndrewPashkin |
Is this expected soon? |
Have retested the OP code with That said, it appears the round trip fails. Perhaps related to issue ( #117 ). In [1]: import enum
...: class MyEnum(enum.Enum):
...: SPAM = 'SPAM'
...:
...: import cloudpickle
...: cloudpickle.loads(cloudpickle.dumps(MyEnum.SPAM))
...:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-3cc75bf49a58> in <module>()
4
5 import cloudpickle
----> 6 cloudpickle.loads(cloudpickle.dumps(MyEnum.SPAM))
/zopt/conda2/envs/nanshenv3/lib/python3.6/enum.py in __new__(metacls, cls, bases, classdict)
133 # save enum items into separate mapping so they don't get baked into
134 # the new class
--> 135 enum_members = {k: classdict[k] for k in classdict._member_names}
136 for name in classdict._member_names:
137 del classdict[name]
AttributeError: 'dict' object has no attribute '_member_names' |
For reference, I started a new similar PR in #246 but as discussed in the comments, I don't think the current cloudpickle behavior is fine. We would need an explicit scoping mechanism to make it possible to load 2 pickle strings that hold instances of the same original dynamically class / types to share the reconstructed type definitions while also making it possible to change redefine classes with the same name in the original |
Now that #246 is merged, we can close this. |
Confirmed as fixed. In [1]: import enum
...: class MyEnum(enum.Enum):
...: SPAM = 'SPAM'
...:
...: import cloudpickle
...: cloudpickle.dumps(MyEnum.SPAM)
Out[1]: b'\x80\x04\x95\xe9\x00\x00\x00\x00\x00\x00\x00\x8c\x17cloudpickle.cloudpickle\x94\x8c\x19_rehydrate_skeleton_class\x94\x93\x94(h\x00\x8c\x13_make_skeleton_enum\x94\x93\x94(\x8c\x04enum\x94\x8c\x04Enum\x94\x93\x94\x85\x94\x8c\x06MyEnum\x94h\t}\x94\x8c\x04SPAM\x94h\x0bs\x8c\x08__main__\x94\x8c a3c0e0e72031415e9f99a4c26e525d65\x94Nt\x94R\x94}\x94(\x8c\n__module__\x94h\x0c\x8c\x07__new__\x94h\x05\x8c\x0cEnum.__new__\x94\x93\x94utRh\x0b\x85\x94R\x94.' Thanks @pierreglaser! |
This fails
Originally reported here: dask/distributed#1178 by @AndrewPashkin
The text was updated successfully, but these errors were encountered: