rust-numpy icon indicating copy to clipboard operation
rust-numpy copied to clipboard

Confusion about `TypeMustMatch` and f64

Open legendre6891 opened this issue 1 year ago • 1 comments

Hi! I have a question about why the following code seems to work without throwing an assert error.

On the Rust side:

#[pyfunction]
fn sum_up<'py>(py: Python<'py>, array: PyArrayLike1<'py, f64, TypeMustMatch>) -> f64 {
    array.as_array().sum()
}

On the python side

>>> A = np.array([2**63, 2**63+1], dtype=np.uint64)
>>> sum_up(A)
1.8446744073709552e+19

It seems like A is being converted to a f64, and then addition is carried out. (For reference, A.sum() = 1.) I thought that the TypeMustMatch means that the array's dtype must be f64.

Either I'm misunderstanding TypeMustMatch or perhaps there is a bug here?

legendre6891 avatar Sep 24 '24 03:09 legendre6891

I think this is related to some functionality I touched on in #491, where there is an attempt to extract PyArrayLike1<T> from Vec<T>.

I'd agree that this is probably not the desired behavior, since AllowTypeChange/TypeMustMatch is only applied to 1D arrays if extraction from a Vec fails.

https://github.com/PyO3/rust-numpy/blob/8bfbb276cb809130cf99a094fdc14a911bc72bda/src/array_like.rs#L152-L161

jakemoran avatar Jun 03 '25 18:06 jakemoran