Skip to content

Commit

Permalink
Do not interpret Enum members called 'description' as description pro…
Browse files Browse the repository at this point in the history
…perties (#1478)

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


Fixes #1321
  • Loading branch information
mike-roberts-healx committed Dec 1, 2022
1 parent f09b2e5 commit a141e84
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion graphene/types/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum as PyEnum
import inspect
from functools import partial

Expand Down Expand Up @@ -169,10 +170,16 @@ def create_enum(graphene_type):
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
):
Expand Down
33 changes: 33 additions & 0 deletions graphene/types/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 comments on commit a141e84

@julianmoji
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mike-roberts-healx, Nice fix I was struggling with it some hours ago, is it already available for version 3.1.1? I've already installed it, but I just can't get your change

@mike-roberts-healx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @mike-roberts-healx, Nice fix I was struggling with it some hours ago, is it already available for version 3.1.1? I've already installed it, but I just can't get your change

Hi @julianmoji I guess it will be available when 3.1.2 is released. I'm not sure when that might be though.

Does graphene have a regular release schedule @erikwrede ?

@erikwrede
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julianmoji
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.