array-api icon indicating copy to clipboard operation
array-api copied to clipboard

result_type() for mixed arrays/Python scalars

Open shoyer opened this issue 9 months ago • 11 comments

The array API's type promotion rules support mixed scalar/array operations, e.g., 1 + xp.arange(3).

For Xarray, we would like to be able to figure out the resulting dtype from this sort of operation before actually doing it (https://github.com/pydata/xarray/pull/8946).

Ideally, we could use xp.result_type() for this purpose, but as documented result_type only supports arrays and dtype objects. Could we potentially extend result_type to also handle Python scalars? It is worth noting that this already works today in NumPy, e.g.,

>>> np.result_type(1, np.arange(3))
dtype('int64')

shoyer avatar May 15 '24 16:05 shoyer