Bears-R-Us / arkouda

Arkouda (αρκούδα): Interactive Data Analytics at Supercomputing Scale :bear:
Other
235 stars 87 forks source link

`array_api` gives unexpected result for `Array._new(x)` when `x` is an `Array`. #3754

Open ajpotts opened 1 week ago

ajpotts commented 1 week ago

Describe the bug Array creation in array_api gives unexpected result for Array._new(x) when x is an Array.

To Reproduce

In [13]: x
Out[13]: Arkouda Array ((10,), int64)[0 1 2 3 4 5 6 7 8 9]

In [14]: y = xp.Array._new(x)

In [15]: type(y)
Out[15]: arkouda.array_api.array_object.Array

In [16]: type(y._array)
Out[16]: arkouda.array_api.array_object.Array
ajpotts commented 1 week ago

I investigated this a bit. It seems like the best solution might be to have an array_like type variable, something like this:

array_like =  Union[numeric_scalars, pdarray, Sequence[pdarray]]

and restrict the Array._array object to the array_like type.

However, this would require a refactor of a number of functions that call the attributes of _array. For example:


def empty_like(x: Array, /, *, dtype: Optional[Dtype] = None, device: Optional[Device] = None) -> Array:
    """
    Return a new array whose shape and dtype match the input array, without initializing entries.
    """
    from .array_object import Array

    if device not in ["cpu", None]:
        raise ValueError(f"Unsupported device {device!r}")

    t = x.dtype if dtype is None else akdtype(dtype)

    return Array._new(
        pdarray(
            "__empty__",
            t,
            x._array.size,
            x._array.ndim,
            x._array.shape,
            x._array.itemsize,
            x._array.max_bits,
        ),
        empty=True,
    )