You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to extend an array type to have some user-defined flags.
Background
Various array libraries provide some mechanisms for extending their arrays. For example, NumPy provides __array_function__ described in NEP 18, and Jax provides an undocumented/unrecommended __jax_array__.
Example
Suppose I would like to add flags to an array type. Would something like this work?
classHasFlags:
def__init__(self, flags: dict[str, Any], **kwargs) ->None:
super().__init__(**kwargs)
self.flags=flagsclassArrayAPIBase: # Perhaps have this in array-api-compat?"""A base class to implement an Array API array by composition."""def__init__(self, array: Array, **kwargs: Any) ->None:
super().__init__(**kwargs)
self.array=arraydef__array_namespace__(self): # noqa: ANN204, PLW3201returnself.array.__array_namespace__dtype=property(lambdaself: self.array.dtype)
shape=property(lambdaself: self.array.shape)
# ...classFlagArray(ArrayAPIBase, HasFlags):
pass
The text was updated successfully, but these errors were encountered:
I think extending by either subclassing or a protocol a la __array_function__ is impossible to standardize - too hard, no commonality between libraries, and has runtime overhead.
What can be done is wrapping with a new array class, stashing the array API standard-compliant array as a property on a wrapper array instance. @mdhaber has an implementation of masked arrays that do such a thing at https://github.com/mdhaber/marray. I believe it's (almost) fully functional.
Problem
I would like to extend an array type to have some user-defined flags.
Background
Various array libraries provide some mechanisms for extending their arrays. For example, NumPy provides
__array_function__
described in NEP 18, and Jax provides an undocumented/unrecommended__jax_array__
.Example
Suppose I would like to add flags to an array type. Would something like this work?
The text was updated successfully, but these errors were encountered: