Skip to content
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

881: Corrected enum metaclass to fix pickle.dumps() #1495

Merged
merged 8 commits into from
Jun 4, 2023
27 changes: 27 additions & 0 deletions graphene/tests/issues/test_881.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pickle

from ...types.enum import Enum


class PickleEnum(Enum):
# is defined outside of test because pickle unable to dump class inside ot pytest function
A = "a"
B = 1


def test_enums_pickling():
a = PickleEnum.A
pickled = pickle.dumps(a)
restored = pickle.loads(pickled)
assert type(a) is type(restored)
assert a == restored
assert a.value == restored.value
assert a.name == restored.name

b = PickleEnum.B
pickled = pickle.dumps(b)
restored = pickle.loads(pickled)
assert type(a) is type(restored)
assert b == restored
assert b.value == restored.value
assert b.name == restored.name
4 changes: 3 additions & 1 deletion graphene/types/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ def __new__(cls, name_, bases, classdict, **options):
# with the enum values.
enum_members.pop("Meta", None)
enum = PyEnum(cls.__name__, enum_members)
return SubclassWithMeta_Meta.__new__(
obj = SubclassWithMeta_Meta.__new__(
cls, name_, bases, dict(classdict, __enum__=enum), **options
)
globals()[name_] = obj.__enum__
return obj

def get(cls, value):
return cls._meta.enum(value)
Expand Down