Skip to content

Added fix for is_nullish() that works with numpy arrays equality test #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/graphql/pyutils/is_nullish.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
from typing import Any

from ..error import INVALID
Expand All @@ -7,4 +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 value != value
return (
value is None
or value is INVALID
or (isinstance(value, float) and math.isnan(value))
)
19 changes: 19 additions & 0 deletions tests/pyutils/test_is_nullish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,3 +45,6 @@ def undefined_is_nullish():

def nan_is_nullish():
assert is_nullish(nan)

def numpy_arrays_are_not_nullish():
assert is_nullish(FakeNumpyArray()) is False