Skip to content

Cannot pickle graphene.Enum #881

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

Closed
kazhuravlev opened this issue Dec 23, 2018 · 6 comments
Closed

Cannot pickle graphene.Enum #881

kazhuravlev opened this issue Dec 23, 2018 · 6 comments
Labels

Comments

@kazhuravlev
Copy link

Environment

Mac OS

$ sw_vers -productVersion 
10.14

$ python -V
Python 3.6.3

$ python -c 'import graphene; print(graphene.__version__)'
2.1.3

Reproduce

import graphene
import pickle

class ExampleEnum(graphene.Enum):
    CAT = 1
    DOG = 2

print(pickle.dumps(ExampleEnum.CAT))
Traceback (most recent call last):
  File "/Users/exanpleuser/scratches/scratch.py", line 9, in <module>
    print(pickle.dumps(ExampleEnum.CAT))
_pickle.PicklingError: Can't pickle <enum 'EnumMeta'>: attribute lookup ExampleEnum on graphene.types.enum failed

Expected

Pickle can perform dumps and loads on graphene.Enum objects

@avivey
Copy link

avivey commented Jan 4, 2019

(I think #879 also makes this stuff work)

@stale
Copy link

stale bot commented Jul 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 29, 2019
@stale stale bot closed this as completed Aug 5, 2019
@senseysensor
Copy link
Contributor

any fix for this...?

@senseysensor
Copy link
Contributor

senseysensor commented Feb 13, 2023

Don't have idea why this gets closed and won't be fixed,
however, fix is very small. This can be both fixed in library source code or if create enums using custom metaclass.

from graphene.types.enum import EnumMeta as GrEnumMeta
import graphene.types.enum


class MyMeta(GrEnumMeta):
    def __new__(cls, name_, bases, classdict, **options):
        obj = super().__new__(cls, name_, bases, classdict, **options)
        setattr(graphene.types.enum, name_, obj.__enum__)
        return obj


class MyEnum(GrEnum, metaclass=MyMeta):
    A = 'a'
    B = 1


gr_enum = MyEnum.B
pickled = pickle.dumps(gr_enum)
restored = pickle.loads(pickled)
assert type(gr_enum) is type(restored)
assert gr_enum == restored

@erikwrede
Copy link
Member

@senseysensor nice fix, if you send in a PR including tests I can review & test 🙂

@senseysensor
Copy link
Contributor

Sorry, unfortunately that solution above appeared to have some limitations. In order pickle.dumps() works fine with such approach, declared Enum names in runtime must be unique, otherwise pickle fails with another error. It's easy to submit similar fix for library code (my first commit in PR), at least that will resolve part of cases (which in case of our project will be acceptable).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants