mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-10 19:56:45 +03:00
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:
parent
f09b2e5a81
commit
a141e848c3
|
@ -1,3 +1,4 @@
|
|||
from enum import Enum as PyEnum
|
||||
import inspect
|
||||
from functools import partial
|
||||
|
||||
|
@ -169,10 +170,16 @@ class TypeMap(dict):
|
|||
values = {}
|
||||
for name, value in graphene_type._meta.enum.__members__.items():
|
||||
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):
|
||||
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(
|
||||
graphene_type._meta.deprecation_reason
|
||||
):
|
||||
|
|
|
@ -565,3 +565,36 @@ def test_iterable_instance_creation_enum():
|
|||
for c in TestEnum:
|
||||
result.append(c.name)
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user