Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass detailed error message to python when parsing an argument fails #2177

Closed
ricohageman opened this issue Feb 21, 2022 · 1 comment
Closed

Comments

@ricohageman
Copy link
Contributor

This issue might be seen as a continuation of #1640, which introduced more elaborated error messages when parsing objects using the derived FromPyObject trait. However, the extended error messages are not properly passed to python when parsing arguments provided by python to a rust function fails.

Consider for example the following case. When passing a wrong type for the value field, the error message contains the fact that value cannot be parsed. However, it fails to explain why it failed to parse value. This is valuable information and thus I would like to get this information out.

Example rust code

#[derive(FromPyObject)]
struct ValueClass {
   value: usize,
}

#[pyfunction]
fn function_to_call_with_value_class(value: ValueClass) {
    // Do stuff with the argument.
}

#[pymodule]
fn module(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(function_to_call_with_value_class, m)?)?;
    Ok(())
}

Example python code

Class ValueClass:
    def __init__(self, value):
        self.value = value

# Should be okay
module.function_to_call_with_value_class(ValueClass(0))  

# Should tell that a string cannot be converted into a integer.
# Currently tells: argument 'struct_arg': failed to extract field ValueClass.value
module.function_to_call_with_value_class(ValueClass("unparsable string")) 

# Should tell that a negative integer cannot be converted into an unsigned integer.
# Currently tells: argument 'struct_arg': failed to extract field ValueClass.value
module.function_to_call_with_value_class(ValueClass(-5)) 
@ricohageman ricohageman changed the title Better Type error messages when FromPyObject fails on complex types Pass detailed error message to python when parsing an argument fails Feb 21, 2022
@ricohageman
Copy link
Contributor Author

This is closed by #2178

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

No branches or pull requests

1 participant