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

Add support for Enum inheritance #1364

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion graphene/types/enum.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum as PyEnum
from typing import Iterable, Type, Optional

from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta

Expand All @@ -18,11 +19,18 @@ def eq_enum(self, other):
class EnumOptions(BaseOptions):
enum = None # type: Enum
deprecation_reason = None
enums = () # type: Iterable[Type[Enum]]


class EnumMeta(SubclassWithMeta_Meta):
def __new__(cls, name_, bases, classdict, **options):
enum_members = dict(classdict, __eq__=eq_enum)
enum_members = dict(__eq__=eq_enum)
meta = classdict.get("Meta", None) # type: Optional[EnumOptions]
if meta and hasattr(meta, 'enums'):
for enum in meta.enums:
enum_members.update(enum.as_dict())
enum_members.update(classdict)

# We remove the Meta attribute from the class to not collide
# with the enum values.
enum_members.pop("Meta", None)
Expand Down Expand Up @@ -106,3 +114,10 @@ def get_type(cls):
is mounted (as a Field, InputField or Argument)
"""
return cls

@classmethod
def as_dict(cls):
return {
enum_meta.name: enum_meta.value
for _, enum_meta in cls._meta.enum.__members__.items()
}
74 changes: 74 additions & 0 deletions graphene/types/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,77 @@ class Query(ObjectType):
assert result.data == {"createPaint": {"color": "RED"}}

assert color_input_value == RGB.RED


def test_enum_inheritance():
class ParentRGB(Enum):
RED = 1

class ChildRGB(Enum):
BLUE = 2

class Meta:
enums = (ParentRGB,)

class Query(ObjectType):
color = ChildRGB(required=True)

schema = Schema(query=Query)
assert str(schema) == dedent(
'''\
type Query {
color: ChildRGB!
}

enum ChildRGB {
RED
BLUE
}
'''
)


def test_multiple_enum_inheritance():
class Parent1RGB(Enum):
RED = 1

class Parent2RGB(Enum):
BLUE = 2

class ChildRGB(Enum):
GREEN = 3

class Meta:
enums = (Parent1RGB, Parent2RGB,)

class Query(ObjectType):
color = ChildRGB(required=True)

schema = Schema(query=Query)
assert str(schema) == dedent(
'''\
type Query {
color: ChildRGB!
}

enum ChildRGB {
RED
BLUE
GREEN
}
'''
)


def test_override_enum_inheritance():
class ParentRGB(Enum):
RED = 1
BLUE = 2

class ChildRGB(Enum):
BLUE = 3

class Meta:
enums = (ParentRGB,)

assert ChildRGB.get(3) != ParentRGB.BLUE