Repository: graphql-python/graphene
Pull request #1461 (2022-09-29 23:44:40)

hashable Enum
It would be convenient for `graphene.Enum` to be hashable the same way `enum.Enum` is so that it can be used as a key in a dict or a member of a set.

codecov[bot]: # [Codecov](https://codecov.io/gh/graphql-python/graphene/pull/1461?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python) Report
Base: **95.88**% // Head: **95.87**% // Decreases project coverage by **`-0.01%`** :warning:
> Coverage data is based on head [(`e70c26a`)](https://codecov.io/gh/graphql-python/graphene/pull/1461?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python) compared to base [(`ee1ff97`)](https://codecov.io/gh/graphql-python/graphene/commit/ee1ff975d71f6590eb6933d76d12054c9839774a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #1461      +/-   ##
==========================================
- Coverage   95.88%   95.87%   -0.02%     
==========================================
  Files          51       50       -1     
  Lines        1726     1720       -6     
==========================================
- Hits         1655     1649       -6     
  Misses         71       71              
```


| [Impacted Files](https://codecov.io/gh/graphql-python/graphene/pull/1461?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python) | Coverage Δ | |
|---|---|---|
| [graphene/types/enum.py](https://codecov.io/gh/graphql-python/graphene/pull/1461/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python#diff-Z3JhcGhlbmUvdHlwZXMvZW51bS5weQ==) | `100.00% <100.00%> (ø)` | |
| [graphene/\_\_init\_\_.py](https://codecov.io/gh/graphql-python/graphene/pull/1461/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python#diff-Z3JhcGhlbmUvX19pbml0X18ucHk=) | | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/graphql-python/graphene/pull/1461?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=graphql-python).


Patch:
diff --git a/graphene/types/enum.py b/graphene/types/enum.py
index e5cc50e..0f68236 100644
--- a/graphene/types/enum.py
+++ b/graphene/types/enum.py
@@ -12,6 +12,10 @@ def eq_enum(self, other):
     return self.value is other
 
 
+def hash_enum(self):
+    return hash(self.name)
+
+
 EnumType = type(PyEnum)
 
 
@@ -22,7 +26,7 @@ class EnumOptions(BaseOptions):
 
 class EnumMeta(SubclassWithMeta_Meta):
     def __new__(cls, name_, bases, classdict, **options):
-        enum_members = dict(classdict, __eq__=eq_enum)
+        enum_members = dict(classdict, __eq__=eq_enum, __hash__=hash_enum)
         # We remove the Meta attribute from the class to not collide
         # with the enum values.
         enum_members.pop("Meta", None)


Test patch:
diff --git a/graphene/types/tests/test_enum.py b/graphene/types/tests/test_enum.py
index 679de16..ab1e093 100644
--- a/graphene/types/tests/test_enum.py
+++ b/graphene/types/tests/test_enum.py
@@ -518,3 +518,28 @@ def test_mutation_enum_input_type():
     assert result.data == {"createPaint": {"color": "RED"}}
 
     assert color_input_value == RGB.RED
+
+
+def test_hashable_enum():
+    class RGB(Enum):
+        """Available colors"""
+
+        RED = 1
+        GREEN = 2
+        BLUE = 3
+
+    color_map = {RGB.RED: "a", RGB.BLUE: "b", 1: "c"}
+
+    assert color_map[RGB.RED] == "a"
+    assert color_map[RGB.BLUE] == "b"
+    assert color_map[1] == "c"
+
+
+def test_hashable_instance_creation_enum():
+    Episode = Enum("Episode", [("NEWHOPE", 4), ("EMPIRE", 5), ("JEDI", 6)])
+
+    trilogy_map = {Episode.NEWHOPE: "better", Episode.EMPIRE: "best", 5: "foo"}
+
+    assert trilogy_map[Episode.NEWHOPE] == "better"
+    assert trilogy_map[Episode.EMPIRE] == "best"
+    assert trilogy_map[5] == "foo"