Skip to content

Confusion about TypeMustMatch and f64 #444

Closed

Description

@legendre6891

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?

Activity

  1. jakemoran commented on Jun 3, 2025

    @jakemoran
    Contributor

    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.

    if matches!(D::NDIM, None | Some(1)) {
    if let Ok(vec) = ob.extract::<Vec<T>>() {
    let array = Array1::from(vec)
    .into_dimensionality()
    .expect("D being compatible to Ix1")
    .into_pyarray(py)
    .readonly();
    return Ok(Self(array, PhantomData));
    }
    }

  2. Icxolu commented on Dec 13, 2025

    @Icxolu
    Member

    Fixed by #520

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions