Do not interpret Enum members called 'description' as description properties (#1478)

This is a workaround for `TypeError`s being raised when initialising schemas with
Enum members named `description` or `deprecation_reason`.


Fixes #1321
This commit is contained in:
Mike Roberts 2022-12-01 10:06:24 +00:00 committed by GitHub
parent f09b2e5a81
commit a141e848c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -1,3 +1,4 @@
from enum import Enum as PyEnum
import inspect import inspect
from functools import partial from functools import partial
@ -169,10 +170,16 @@ class TypeMap(dict):
values = {} values = {}
for name, value in graphene_type._meta.enum.__members__.items(): for name, value in graphene_type._meta.enum.__members__.items():
description = getattr(value, "description", None) description = getattr(value, "description", None)
deprecation_reason = getattr(value, "deprecation_reason", None) # if the "description" attribute is an Enum, it is likely an enum member
# called description, not a description property
if isinstance(description, PyEnum):
description = None
if not description and callable(graphene_type._meta.description): if not description and callable(graphene_type._meta.description):
description = graphene_type._meta.description(value) description = graphene_type._meta.description(value)
deprecation_reason = getattr(value, "deprecation_reason", None)
if isinstance(deprecation_reason, PyEnum):
deprecation_reason = None
if not deprecation_reason and callable( if not deprecation_reason and callable(
graphene_type._meta.deprecation_reason graphene_type._meta.deprecation_reason
): ):

View File

@ -565,3 +565,36 @@ def test_iterable_instance_creation_enum():
for c in TestEnum: for c in TestEnum:
result.append(c.name) result.append(c.name)
assert result == expected_values assert result == expected_values
# https://github.com/graphql-python/graphene/issues/1321
def test_enum_description_member_not_interpreted_as_property():
class RGB(Enum):
"""Description"""
red = "red"
green = "green"
blue = "blue"
description = "description"
deprecation_reason = "deprecation_reason"
class Query(ObjectType):
color = RGB()
def resolve_color(_, info):
return RGB.description
values = RGB._meta.enum.__members__.values()
assert sorted(v.name for v in values) == [
"blue",
"deprecation_reason",
"description",
"green",
"red",
]
schema = Schema(query=Query)
results = schema.execute("query { color }")
assert not results.errors
assert results.data["color"] == RGB.description.name