Open andersy005 opened 9 months ago
Note that on python 3.12 and higher this will actually start to work because it will not use hasattr
anymore (which, by calling getattr
, evaluates properties / descriptors).
See also #8605
Isn't this a bug in sparse?
import numpy as np
import numpy.array_api as xp
import sparse
a_np = np.array(['a', 'b']).astype(object)
a_sparse = sparse.COO(a_np)
print(a_np.real) # works
print(a_sparse.real) # ValueError: invalid literal for int() with base 10: 'a'
a_xp = xp.asarray(a_np) # TypeError: The array_api namespace does not support the dtype 'object'
is_duck_array
looks like this:
https://github.com/pydata/xarray/blob/c9ba2be2690564594a89eb93fb5d5c4ae7a9253c/xarray/core/utils.py#L262-L273
It is simply not true that namedarray only needs these for everything to work as namedarray use .real
, .imag
etc.
The true minimum requirement is _arrayfunction_or_api
.
By using _arrayfunction_or_api
you will guarantee that mypy finds places where namedarray uses more methods than the requirement and this happens quite often as there are many places where we assume (incorrectly?) numpy arrays are used.
What happened?
i'm experiencing compatibility issues when using
_arrayfunction_or_api
and_sparsearrayfunction_or_api
with the sparse arrays withdtype=object
. specifically, runtime checks usingisinstance
with these protocols are failing, despite the sparse array object appearing to meet the necessary criteria (attributes and methods).What did you expect to happen?
i expected that since COO arrays from the sparse library provide the necessary attributes and methods, they would pass the
isinstance
checks with the defined protocols.the failing case appears to be a well know issue
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
i was trying to replace instances of
is_duck_array
with the protocol runtime checks (as part of https://github.com/pydata/xarray/pull/8319), and i've come to a realization that these runtime checks are rigid to accommodate the diverse behaviors of different array types, andis_duck_array()
the function-based approach might be more manageable.@Illviljan, are there any changes that could be made to both protocols without making them too complex?
Environment