question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using the standard enum.Enum causes TypeError

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
syrusakbarycommented, Sep 16, 2017

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())
0reactions
RuiLoureirocommented, Oct 14, 2021

I also came across this bug. Why does graphene try to sort the arguments by the argument value (f[1]), instead of the argument name (f[0])? https://github.com/graphql-python/graphene/blob/0a54094f59e1b1bca83e4574dbb35587536bbce6/graphene/types/argument.py#L79

Read more comments on GitHub >

github_iconTop Results From Across the Web

enum — Support for enumerations — Python 3.11.1 ...
In Python 3.12 it will be possible to check for member values and not just members; until then, a TypeError will be raised...
Read more >
Associating string representations with an Enum that uses ...
It seems that the issue is that an Enum will use all class variables as the enum values, which causes them to return...
Read more >
Build Enumerations of Constants With Python's Enum
In this tutorial, you'll learn how to: Create enumerations of constants using Python's Enum class; Work with enumerations and their members in ...
Read more >
enum – Enumeration Type — PyMOTW 3
Members with repeated values trigger a ValueError exception when the Enum class is being interpreted. $ python3 enum_unique_enforce.py Traceback ...
Read more >
Python Enumeration
Python provides you with the enum module that contains the Enum type for ... to assign a new member to the Color enumeration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found