RFC: #[derive(PyException)] macro for easier error interop between Rust and Python#4186
RFC: #[derive(PyException)] macro for easier error interop between Rust and Python#4186
#[derive(PyException)] macro for easier error interop between Rust and Python#4186Conversation
|
This is a very interesting idea! I hadn't thought of using a I think a potential additional complication is how to add I think my biggest worry here is that it seems like a convenience which risks being opaque to users (we'd have to document it well) and is quite tricky to refactor gradually. If users need to add a little bit more detail or complexity to their Python exception like methods or data then they have to escape into the full-blown manual implementation. I am excited for innovation in this space, though - removing Would also be interested in hearing others' reactions. I think I want to learn more about how this feels and am not confident enough yet, so the initial thought is this makes sense to be an experimental feature or separate crate so that we get time to learn from it. Regarding the ABI3 limitation - that's still just technical deficiency in PyO3 and we could sort that given a bit of time investment. |
I had an idea about how we could potentially make working with between Rust error types and custom Python exceptions easier using a derive macro. This is a very experimental/crude WIP implementation of that idea. This provides a
#[derive(PyException)]derive macro to generate the necessary conversion glue code between the original Rust error (structorenum) and (generated) custom exceptions (implemented as unit structs).The basic idea is to desugar the following (some ordinary Rust error type implementing
std::error::Error)into
This allow using the Rust error type completely normally, while still throwing the corresponding exception on the Python side.
For enums this will create a base exception (catch all) and an specific exception for each variant:
desugars into
This could remove a lot of repetitive boilerplate for the common path, while really complex structures might still require implementing the pattern above manually.
Extensions / Drawbacks
create_exception!in that case)What do people think of that idea? Is it worth exploring further?
Xref: #295