Repository navigation
What's your problem? #22
Description
Let's collect a bunch of code samples from array-api libraries that array-api-typing could improve. That should help give us a better idea of what is needed. They should also be useful for (integration) testing.
Problems
scipy-stubs
See scipy/scipy-stubs#140 for all of SciPy's functions that have (at least some) support for the Array API.
Here's an example from the scipy docs
import torch
from scipy.cluster.vq import vq
code_book = torch.tensor([[1., 1., 1.],
[2., 2., 2.]])
features = torch.tensor([[1.9, 2.3, 1.7],
[1.5, 2.5, 2.2],
[0.8, 0.6, 1.7]])
code, dist = vq(features, code_book)
print(code) # tensor([1, 1, 0], dtype=torch.int32)
print(dist) # tensor([0.4359, 0.7348, 0.8307])There is currently no good way to annotate this behavior in scipy-stubs. Currently, with scipy-stubs==1.15.3.0, code and dist are inferred as 1d numpy arrays of int and float dtypes, respectively. The reason for this is the torch.tensor.__array__ method, cuasing it to be interpreted as a numpy "array-like".
efax
By @NeilGirdhar from #22 (comment)
Consider practically any function in my efax library. For example, we have:
def _r_s_mu(self) -> tuple[JaxComplexArray, JaxRealArray, JaxComplexArray]:
xp = array_namespace(self)
r = -self.pseudo_precision / self.negative_precision
s = xp.reciprocal((abs_square(r) - 1.0) * self.negative_precision)
k = self.pseudo_precision / self.negative_precision
l_eta = 0.5 * self.mean_times_precision / ((abs_square(k) - 1.0) * self.negative_precision)
mu = xp.conj(l_eta) - xp.conj(self.pseudo_precision / self.negative_precision) * l_eta
return r, s, mu
This is barely type-checked. It would be nice if xp: ArrayNamespace, r, s, k, l_eta, mu: Array, etc.
Activity
NeilGirdhar commentedon Jun 14, 2025
Consider practically any function in my efax library. For example, we have:
This is barely type-checked. It would be nice if
xp: ArrayNamespace,r, s, k, l_eta, mu: Array, etc.Also, it would be nice to have dtype support. Jax doesn't have it, so the placeholders I'm using don't have any effect.
jorenham commentedon Jun 15, 2025
What would that (roughly) look like in your example?
NeilGirdhar commentedon Jun 15, 2025
Ideally, just this:
And ideally, everything would be type checked.
Even better would be to make all of the classes in Efax generic on the array library somehow, and then you could have:
Not sure if that's possible, but that would be ideal. Maybe with Ty's intersection types, we could somehow intersect the
xpt.Array[Real]withjax.Array?jorenham commentedon Jun 15, 2025
The
# Or however this is done.is what will make up 75% of array-api-typing, in my estimation.The big challenge is for array-api-typing to figure out what the type of
ComplexDTypeis, and in such a way that tymypyflyright will also be able to understand it.And the solution I had in mind for that, involves something like structural
HasArrayNamespacetype, and a bunch of other protocols such asHasBoolDType[_, _]. You can then obtain the actual dtype typeTby matching it againstHasBoolDType[HasArrayNamespace, T]in your function signature.That's a hard requirement, as far as I'm concerned :)
Intersection types will indeed be very useful when dealing with structural typing. But until the PEP is finished and accepted, we won't be able to use it, unfortunately. There has been some recent development on it: CarliJoy/intersection_examples#53.
I think we'll be able to manage without intersection types, though.
NeilGirdhar commentedon Jun 15, 2025
I just hope this doesn't become a mess of templates. That would be a large cost in itself. Maybe we should sketch out what my code would look like under this system?
nstarman commentedon Aug 22, 2025
Can/Has{X}Protocols for the methods and attributes of the array object in the Array API. I want granular descriptions of what my functions need, e.g. (very loosely)Arrayand partially filled in types that describe numerical versus boolean arrays.Does{X}Protocols for the functions in the Array API.ArrayNamespace.ArrayandArrayNamespacework together 🤝 .lucascolley commentedon Aug 22, 2025
I've had multiple different people asking about shape-typing at EuroSciPy btw :)