Graphene: Using the standard enum.Enum causes TypeError

Created on 13 Sep 2017  路  5Comments  路  Source: graphql-python/graphene

I'm getting the following errors when I try to use the vanilla enum in Python 3.6 + graphene 2.0.dev20170802065539:

class KernelStatus(enum.Enum):
    PREPARING = 10
    RUNNING = 30
    ...

class ComputeSession(graphene.ObjectType):
    status = graphene.Enum.from_enum(KernelStatus)
    ...

class Query(graphene.ObjectType):
    compute_sessions = graphene.List(ComputeSession,
        access_key=graphene.String(required=True),
        status=graphene.Enum.from_enum(KernelStatus))
    ...

Error during initialization:

  File "/Users/joongi/Projects/Lablup/sorna-gateway/sorna/gateway/admin.py", line 92, in <module>
    class Query(graphene.ObjectType):
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/utils/subclass_with_meta.py", line 40, in __init_subclass__
    super_class.__init_subclass_with_meta__(**options)
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/objecttype.py", line 39, in __init_subclass_with_meta__
    yank_fields_from_attrs(base.__dict__, _as=Field)
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/utils.py", line 31, in yank_fields_from_attrs
    field = get_field_as(value, _as)
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/utils.py", line 21, in get_field_as
    return _as.mounted(value)
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/mountedtype.py", line 20, in mounted
    **unmounted.kwargs
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/field.py", line 53, in __init__
    self.args = to_arguments(args or OrderedDict(), extra_args)
  File "/Users/joongi/Projects/Lablup/sorna-gateway/venv/lib/python3.6/site-packages/graphene/types/argument.py", line 41, in to_arguments
    extra_args = sorted(extra_args.items(), key=lambda f: f[1])
TypeError: '<' not supported between instances of 'EnumMeta' and 'String'

So, is there no way to use the standard enum.Enum class in Python 3?
Or am I missing something?
I need to use the vanilla one because I share the same enum type with a user-defined TypeDecorator in SQLAlchemy core.

Most helpful comment

Hi @achimnol, you will need to initialize the Enum (or wrap on a Field) when using it:

class KernelStatus(enum.Enum):
    PREPARING = 10
    RUNNING = 30
    ...

KernelStatusEnum = graphene.Enum.from_enum(KernelStatus)

class ComputeSession(graphene.ObjectType):
    status = KernelStatusEnum()
    ...

class Query(graphene.ObjectType):
    compute_sessions = graphene.List(ComputeSession,
        access_key=graphene.String(required=True),
        status=KernelStatusEnum())

All 5 comments

Tried converting my enum to graphene.pyutils.enum.Enum but the same error persists... 馃槥

It seems that the pure graphene.Enum also does not work with the same error.

Hi @achimnol, you will need to initialize the Enum (or wrap on a Field) when using it:

class KernelStatus(enum.Enum):
    PREPARING = 10
    RUNNING = 30
    ...

KernelStatusEnum = graphene.Enum.from_enum(KernelStatus)

class ComputeSession(graphene.ObjectType):
    status = KernelStatusEnum()
    ...

class Query(graphene.ObjectType):
    compute_sessions = graphene.List(ComputeSession,
        access_key=graphene.String(required=True),
        status=KernelStatusEnum())

Hi @achimnol . We're currently going through old issues that appear to have gone stale (ie. not updated in about the last 6 months) to try and clean up the issue tracker. If this is still important to you please comment and we'll re-open this.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings