Skip to content

Commit a8eccee

Browse files
Do not interpret Enum members called 'description' as description properties
This is a workaround for `TypeError`s being raised when initialising schemas with Enum members named `description` or `deprecation_reason`. It is still not ideal as you can't have an enum with both a "description" member and a description property. Fixing this would require some more thought though. Potentially we could allow something like `__description__` as an alternative way of naming the description property in cases where it conflicts with one of the members. Fixes #1321
1 parent 7f6fa16 commit a8eccee

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

graphene/types/schema.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum as PyEnum
12
import inspect
23
from functools import partial
34

@@ -169,10 +170,14 @@ def create_enum(graphene_type):
169170
values = {}
170171
for name, value in graphene_type._meta.enum.__members__.items():
171172
description = getattr(value, "description", None)
172-
deprecation_reason = getattr(value, "deprecation_reason", None)
173+
# if the "description" attribute is an Enum, it is likely an enum member
174+
# called description, not a description property
175+
description = None if isinstance(description, PyEnum) else description
173176
if not description and callable(graphene_type._meta.description):
174177
description = graphene_type._meta.description(value)
175178

179+
deprecation_reason = getattr(value, "deprecation_reason", None)
180+
deprecation_reason = None if isinstance(deprecation_reason, PyEnum) else deprecation_reason
176181
if not deprecation_reason and callable(
177182
graphene_type._meta.deprecation_reason
178183
):

graphene/types/tests/test_enum.py

+33
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,36 @@ def test_iterable_instance_creation_enum():
565565
for c in TestEnum:
566566
result.append(c.name)
567567
assert result == expected_values
568+
569+
570+
# https://github.com/graphql-python/graphene/issues/1321
571+
def test_enum_description_member_not_interpreted_as_property():
572+
class RGB(Enum):
573+
"""Description"""
574+
575+
red = "red"
576+
green = "green"
577+
blue = "blue"
578+
description = "description"
579+
deprecation_reason = "deprecation_reason"
580+
581+
class Query(ObjectType):
582+
color = RGB()
583+
584+
def resolve_color(_, info):
585+
return RGB.description
586+
587+
values = RGB._meta.enum.__members__.values()
588+
assert sorted(v.name for v in values) == [
589+
"blue",
590+
"deprecation_reason",
591+
"description",
592+
"green",
593+
"red",
594+
]
595+
596+
schema = Schema(query=Query)
597+
598+
results = schema.execute("query { color }")
599+
assert not results.errors
600+
assert results.data["color"] == RGB.description.name

0 commit comments

Comments
 (0)