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.

Serializing native Python enums does not work

See original GitHub issue

Currently using SQL enums works well, unless you use a enum.Enum as base for the enum. For example using this model:

class Hairkind(enum.Enum):
    LONG = 'long'
    SHORT = 'short'

class Pet(Base):
    __tablename__ = 'pets'
    id = Column(Integer(), primary_key=True)
    hair_kind = Column(Enum(Hairkind, name='hair_kind'), nullable=False)

will fail badly if you try to query the hairKind field:

  File "lib/python3.7/site-packages/graphql/execution/executor.py", line 622, in complete_leaf_value
    path=path,
graphql.error.base.GraphQLError: Expected a value of type "hair_kind" but received: Hairkind.LONG

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
wichertcommented, Jul 31, 2018

What is happening here is that graphqln_sqlalchemy.converter.convert_sqlalchemy_type.register creates a GraphQL Enum using the enum instances using items = type.enum_class.__members__.items(). But in graphql.type.definition.GraphQLEnumType.serialize this happens:

    def serialize(self, value):
        # type: (Union[str, PyEnum]) -> Optional[str]
        if isinstance(value, PyEnum):
            # We handle PyEnum values
            value = value.value
        if isinstance(value, collections.Hashable):
            enum_value = self._value_lookup.get(value)
            if enum_value:
                return enum_value.name

        return None

at that point the enum.Enum instance is converted to its value, which is then looked up. But the lookup table has the enum instances, causing the lookup to fail and an error to be returned.

0reactions
robjamparcommented, Aug 30, 2018

Yeah this has broken enums in one of my libraries and the only way i can fix it is to monkey patch the method!

id suggest a non breaking change, for example:

def serialize(self, value):
    if isinstance(value, collections.Hashable):
        if value in self._value_lookup:
            enum_value = self._value_lookup.get(value)
    
        elif isinstance(value, PyEnum):
            # We handle PyEnum values
            enum_value = self._value_lookup.get(value.value)
    
        if enum_value:
            return enum_value.name
    
    return None```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serialising an Enum member to JSON - Stack Overflow
I just went through this exact problem and discovered if you're using ... JSON serializable names # https://docs.python.org/3/library/enum.
Read more >
enum.IntEnum is not compatible with JSON serialisation
Messages (50). msg191467 - (view), Author: Nick Coghlan (ncoghlan) * (Python committer), Date: 2013-06-19 13:45.
Read more >
Python Generated Code | Protocol Buffers - Google Developers
If you assign a value that is not in the enum, the generated code will throw an exception. proto3 uses open enum semantics:...
Read more >
MessagePack: It's like JSON. but fast and small.
MessagePack is an efficient binary serialization format. It lets you exchange data ... If you want to use msgpack at OCaml, you need...
Read more >
PHP 8.1: Enums
Note that due to class-semantics of Enums, class_exists function also returns true for an Enum. UnitEnum interface. Enums that are not backed with...
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