From afdf9df4eb63561337d07c479aad182d23ce322a Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Wed, 29 May 2019 20:23:10 -0700 Subject: [PATCH 1/5] Implement __hash__ for GraphQLError --- graphql/error/graphql_error.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graphql/error/graphql_error.py b/graphql/error/graphql_error.py index d7a71a35..0560a740 100644 --- a/graphql/error/graphql_error.py +++ b/graphql/error/graphql_error.py @@ -165,6 +165,9 @@ def __eq__(self, other): ) ) + def __hash__(self): + return hash(self.original_error) + def __ne__(self, other): return not self.__eq__(other) From dac9396d7f2f1ce41edb52202d3dae43aa05d654 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Wed, 29 May 2019 20:38:08 -0700 Subject: [PATCH 2/5] Add unit test --- tests/error/test_graphql_error.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/error/test_graphql_error.py b/tests/error/test_graphql_error.py index 13960624..77761d31 100644 --- a/tests/error/test_graphql_error.py +++ b/tests/error/test_graphql_error.py @@ -126,3 +126,11 @@ def default_error_formatter_includes_extension_fields(): "path": None, "extensions": {"foo": "bar"}, } + + def is_hashable(): + try: + raise RuntimeError("original") + except RuntimeError as e: + original = e + e = GraphQLError("msg", original_error=original) + assert hash(e) == hash(original) From 5a600464b3bb345090fe3f90aa4e4e41ad6daa79 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Fri, 31 May 2019 10:41:33 -0700 Subject: [PATCH 3/5] Preserve superclass hash functionality --- graphql/error/graphql_error.py | 2 +- tests/error/test_graphql_error.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/graphql/error/graphql_error.py b/graphql/error/graphql_error.py index 0560a740..007d65bd 100644 --- a/graphql/error/graphql_error.py +++ b/graphql/error/graphql_error.py @@ -166,7 +166,7 @@ def __eq__(self, other): ) def __hash__(self): - return hash(self.original_error) + return super(GraphQLError, self).__hash__() def __ne__(self, other): return not self.__eq__(other) diff --git a/tests/error/test_graphql_error.py b/tests/error/test_graphql_error.py index 77761d31..bf3eafed 100644 --- a/tests/error/test_graphql_error.py +++ b/tests/error/test_graphql_error.py @@ -128,9 +128,10 @@ def default_error_formatter_includes_extension_fields(): } def is_hashable(): - try: - raise RuntimeError("original") - except RuntimeError as e: - original = e - e = GraphQLError("msg", original_error=original) - assert hash(e) == hash(original) + e = GraphQLError("msg", extensions={"foo": "bar"}) + assert hash(e) is not Nonw + + def hashes_are_unique(): + e1 = GraphQLError("msg", extensions={"foo": "bar"}) + e2 = GraphQLError("msg", extensions={"foo": "bar"}) + assert hash(e1) != hash(e2) From ac63f966fd0a8c8e3885097b16dca0f02d77852b Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Fri, 31 May 2019 14:23:54 -0700 Subject: [PATCH 4/5] Don't try to support python 2 --- graphql/error/graphql_error.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/graphql/error/graphql_error.py b/graphql/error/graphql_error.py index 007d65bd..96c83f4c 100644 --- a/graphql/error/graphql_error.py +++ b/graphql/error/graphql_error.py @@ -79,6 +79,8 @@ class GraphQLError(Exception): "extensions", ) + __hash__ = Exception.__hash__ + def __init__( self, message: str, @@ -89,7 +91,7 @@ def __init__( original_error: Exception = None, extensions: Dict[str, Any] = None, ) -> None: - super(GraphQLError, self).__init__(message) + super().__init__(message) self.message = message if nodes and not isinstance(nodes, list): nodes = [nodes] # type: ignore @@ -165,9 +167,6 @@ def __eq__(self, other): ) ) - def __hash__(self): - return super(GraphQLError, self).__hash__() - def __ne__(self, other): return not self.__eq__(other) From d2121cc380513218bac131480f15fcf86b7a65af Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Fri, 31 May 2019 14:25:18 -0700 Subject: [PATCH 5/5] Clarify test assertions --- tests/error/test_graphql_error.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/error/test_graphql_error.py b/tests/error/test_graphql_error.py index bf3eafed..80a083c3 100644 --- a/tests/error/test_graphql_error.py +++ b/tests/error/test_graphql_error.py @@ -128,10 +128,9 @@ def default_error_formatter_includes_extension_fields(): } def is_hashable(): - e = GraphQLError("msg", extensions={"foo": "bar"}) - assert hash(e) is not Nonw + hash(GraphQLError("msg")) - def hashes_are_unique(): - e1 = GraphQLError("msg", extensions={"foo": "bar"}) - e2 = GraphQLError("msg", extensions={"foo": "bar"}) + def hashes_are_unique_per_instance(): + e1 = GraphQLError("msg") + e2 = GraphQLError("msg") assert hash(e1) != hash(e2)