Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enum value equality comparison #1525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphene/types/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
def eq_enum(self, other):
if isinstance(other, self.__class__):
return self is other
return self.value is other
if isinstance(other, PyEnum):
# Identical values from different Enum classes are not equal.
return False
return self.value == other


def hash_enum(self):
Expand Down
19 changes: 19 additions & 0 deletions graphene/types/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ class RGB2(Enum):
assert RGB1.BLUE != RGB2.BLUE


def test_enum_to_value_comparison():
class RGB(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"

assert "red" == RGB.RED
assert "red" != RGB.GREEN
assert "red" != RGB.BLUE

assert "green" != RGB.RED
assert "green" == RGB.GREEN
assert "green" != RGB.BLUE

assert "blue" != RGB.RED
assert "blue" != RGB.GREEN
assert "blue" == RGB.BLUE


def test_enum_skip_meta_from_members():
class RGB1(Enum):
class Meta:
Expand Down