From fcbb04eeb10494371f8dc46346e66dbcbf79a404 Mon Sep 17 00:00:00 2001 From: CJ Gasper Date: Fri, 1 Sep 2023 15:55:54 -0400 Subject: [PATCH] Fix enum value equality comparison --- graphene/types/enum.py | 5 ++++- graphene/types/tests/test_enum.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/graphene/types/enum.py b/graphene/types/enum.py index d3469a15..37988568 100644 --- a/graphene/types/enum.py +++ b/graphene/types/enum.py @@ -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): diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py index e6fce66c..79f57681 100644 --- a/graphene/types/tests/test_enum.py +++ b/graphene/types/tests/test_enum.py @@ -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: