-
Notifications
You must be signed in to change notification settings - Fork 128
Is there a way to validate the input dtype? #479
-
Thanks for this amazing project!. I'm exposing this function to python, this has to be the more informative it can, for example internally my Rust code uses an Array1<f64>, but I let the user pass PyReadonlyArrayDyn<'py, f64> instead of PyReadonlyArray1<'py, f64> because I want to raise the proper error when the dimension is not 1. My problem, and looks like it's difficult, it's to control the dtype, If the user gives to the function something like np.array([1,0]) which is not float, I get this error
TypeError: argument 'individual': 'ndarray' object cannot be converted to 'PyArray<T, D>'
Which is too generic. Is validate directly in the python code the only solution for this?. My code snippet is below, thanks
#[pyo3(signature = (individual, seed=None))] fn mutate<'py>( &self, py: Python<'py>, individual: PyReadonlyArrayDyn<'py, f64>, seed: Option<u64>, ) -> PyResult<Bound<'py, PyArray1<f64>>> { // Create an owned array from the input so it lives long enough. let owned_individual = individual.to_owned_array(); let mut owned_individual = owned_individual .into_dimensionality::<Ix1>() .map_err(|_| PyValueError::new_err("Array Must be 1D."))?; /// More code omitted for simplicity
Beta Was this translation helpful? Give feedback.