From a141e848c3f3a70628272028fc63f7a4226029d2 Mon Sep 17 00:00:00 2001 From: Mike Roberts <110839704+mike-roberts-healx@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:06:24 +0000 Subject: [PATCH] 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 --- graphene/types/schema.py | 9 ++++++++- graphene/types/tests/test_enum.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/graphene/types/schema.py b/graphene/types/schema.py index 1a33a93d..7fa046dd 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -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 ): diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py index 298cc233..9b3082df 100644 --- a/graphene/types/tests/test_enum.py +++ b/graphene/types/tests/test_enum.py @@ -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