Closed jenshnielsen closed 3 days ago
Hm yea, that's clearly wrong 🤔
It should be
tuple[float, np.int_] | tuple[npt.NDArray[np.float64], npt.NDArray[np.int_]]
right?
Looking at the Cython code in _ckdtree.pyx it seems that it is
tuple[float, int] | tuple[npt.NDArray[np.float64], npt.NDArray[np.intp]]
The return type is defined as
np.intp_t [:, ::1] ii = np.empty((n,len(k)),dtype=np.intp)
...
iiret = np.reshape(ii, retshape + (len(k),))
bool single = (x_arr.ndim == 1)
...
if single:
ddret = float(ddret)
iiret = int(iiret)
Hmmm, when I ran it as tree.query([0, 2.2])
it returned
(0.20000000000000018, np.int64(0))
(I'm on a 64-bit linux platform, so intp
is an alias for int64
)
So maybe that has to do with the fact that builtins.int
isn't fixed-width?
Anyway, I think I'll just play it safe, and use int | np.int_
instead 🤷🏻. It shouldn't matter much in practice.
And you're right about the intp
dtype: I forgot that int_
isn't the same as intp
on numpy<2
, which we still support.
According to the scipy docs query should return float, int or array of float, array of int
https://github.com/scipy/scipy/blob/main/scipy/spatial/_kdtree.py#L395
However, in scipy-stubs these are both typed as returning a tuple of two floats or tuple of arrays of floats
https://github.com/jorenham/scipy-stubs/blob/master/scipy-stubs/spatial/_ckdtree.pyi#L88
https://github.com/jorenham/scipy-stubs/blob/master/scipy-stubs/spatial/_kdtree.pyi#L73