From 938dc9d5da39bada2c231e170e4b789a9b556341 Mon Sep 17 00:00:00 2001 From: Tom Cobb Date: Fri, 13 Sep 2019 12:30:01 +0100 Subject: [PATCH 1/2] Added fix for is_nullish() that works with numpy arrays equality test --- src/graphql/pyutils/is_nullish.py | 4 +++- tests/pyutils/test_is_nullish.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/graphql/pyutils/is_nullish.py b/src/graphql/pyutils/is_nullish.py index 3e4f2a0d..a4813344 100644 --- a/src/graphql/pyutils/is_nullish.py +++ b/src/graphql/pyutils/is_nullish.py @@ -1,4 +1,5 @@ from typing import Any +from math import isnan from ..error import INVALID @@ -7,4 +8,5 @@ def is_nullish(value: Any) -> bool: """Return true if a value is null, undefined, or NaN.""" - return value is None or value is INVALID or value != value + return value is None or value is INVALID or ( + isinstance(value, float) and isnan(value)) diff --git a/tests/pyutils/test_is_nullish.py b/tests/pyutils/test_is_nullish.py index 9e71b00a..dbcf1d40 100644 --- a/tests/pyutils/test_is_nullish.py +++ b/tests/pyutils/test_is_nullish.py @@ -4,6 +4,22 @@ from graphql.pyutils import is_nullish +class FakeNumpyArray: + def __eq__(self, other): + # Numpy arrays return an array when compared with another numpy array + # containing the pointwise equality of the two + if isinstance(other, FakeNumpyArray): + return FakeNumpyArray() + else: + return False + + def __bool__(self): + raise TypeError( + "The truth value of an array with more than one element is " + "ambiguous. Use a.any() or a.all()" + ) + + def describe_is_nullish(): def null_is_nullish(): assert is_nullish(None) is True @@ -29,3 +45,7 @@ def undefined_is_nullish(): def nan_is_nullish(): assert is_nullish(nan) + + def numpy_arrays_are_not_nullish(): + assert is_nullish(FakeNumpyArray()) is False + From 2560d54b7443bd7be95ca1d702ad5adf723c95e1 Mon Sep 17 00:00:00 2001 From: Tom Cobb Date: Fri, 13 Sep 2019 13:22:47 +0100 Subject: [PATCH 2/2] Make black happy... --- src/graphql/pyutils/is_nullish.py | 9 ++++++--- tests/pyutils/test_is_nullish.py | 1 - 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/graphql/pyutils/is_nullish.py b/src/graphql/pyutils/is_nullish.py index a4813344..6ccd42d9 100644 --- a/src/graphql/pyutils/is_nullish.py +++ b/src/graphql/pyutils/is_nullish.py @@ -1,5 +1,5 @@ +import math from typing import Any -from math import isnan from ..error import INVALID @@ -8,5 +8,8 @@ def is_nullish(value: Any) -> bool: """Return true if a value is null, undefined, or NaN.""" - return value is None or value is INVALID or ( - isinstance(value, float) and isnan(value)) + return ( + value is None + or value is INVALID + or (isinstance(value, float) and math.isnan(value)) + ) diff --git a/tests/pyutils/test_is_nullish.py b/tests/pyutils/test_is_nullish.py index dbcf1d40..7bad7763 100644 --- a/tests/pyutils/test_is_nullish.py +++ b/tests/pyutils/test_is_nullish.py @@ -48,4 +48,3 @@ def nan_is_nullish(): def numpy_arrays_are_not_nullish(): assert is_nullish(FakeNumpyArray()) is False -