Skip to content

Custom scalars using numpy arrays fail is_nullish() test #59

Closed
@thomascobb

Description

@thomascobb

I have a custom scalar type for a numpy array:

def serialize_ndarray(value):
    return dict(
        numberType=value.dtype.name.upper(),
        base64=base64.b64encode(value).decode()
    )

ndarray_type = GraphQLScalarType("NDArray", serialize=serialize_ndarray)

is_nullish() does an inequality test on values to work out if they are NaN:

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

Unfortunately, numpy arrays break this because they override __eq__ to return a pointwise equality array rather than a boolean.

The comment for is_nullish() suggests that value != value is checking for NaN. If this is the case we could replace it with:

def is_nan(value):
    """Return true if a value is NaN"""
    try:
        return math.isnan(value)
    except TypeError:
        return False

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 is_nan(value)

Would this be acceptable? I can provide a PR if it is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions