mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-18 12:02:19 +03:00
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
This commit is contained in:
parent
7f6fa16194
commit
1e1280076c
|
@ -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
|
||||||
):
|
):
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user