Skip to content

Commit c3f47da

Browse files
coretlCito
authored andcommitted
Added fix for is_nullish() that works with numpy arrays equality test (#60)
1 parent 51a933f commit c3f47da

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/graphql/pyutils/is_nullish.py

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

34
from ..error import INVALID
@@ -7,4 +8,8 @@
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 (
12+
value is None
13+
or value is INVALID
14+
or (isinstance(value, float) and math.isnan(value))
15+
)

tests/pyutils/test_is_nullish.py

Lines changed: 19 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,6 @@ 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

0 commit comments

Comments
 (0)