Skip to content

Commit 938dc9d

Browse files
committed
Added fix for is_nullish() that works with numpy arrays equality test
1 parent c423676 commit 938dc9d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/graphql/pyutils/is_nullish.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
from math import isnan
23

34
from ..error import INVALID
45

@@ -7,4 +8,5 @@
78

89
def is_nullish(value: Any) -> bool:
910
"""Return true if a value is null, undefined, or NaN."""
10-
return value is None or value is INVALID or value != value
11+
return value is None or value is INVALID or (
12+
isinstance(value, float) and isnan(value))

tests/pyutils/test_is_nullish.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
from graphql.pyutils import is_nullish
55

66

7+
class FakeNumpyArray:
8+
def __eq__(self, other):
9+
# Numpy arrays return an array when compared with another numpy array
10+
# containing the pointwise equality of the two
11+
if isinstance(other, FakeNumpyArray):
12+
return FakeNumpyArray()
13+
else:
14+
return False
15+
16+
def __bool__(self):
17+
raise TypeError(
18+
"The truth value of an array with more than one element is "
19+
"ambiguous. Use a.any() or a.all()"
20+
)
21+
22+
723
def describe_is_nullish():
824
def null_is_nullish():
925
assert is_nullish(None) is True
@@ -29,3 +45,7 @@ def undefined_is_nullish():
2945

3046
def nan_is_nullish():
3147
assert is_nullish(nan)
48+
49+
def numpy_arrays_are_not_nullish():
50+
assert is_nullish(FakeNumpyArray()) is False
51+

0 commit comments

Comments
 (0)