Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there a way to validate the input dtype? #479

Unanswered
andresliszt asked this question in Q&A
Discussion options

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
 
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /