RFC: #[derive(PyException)]
macro for easier error interop between Rust and Python
#4186
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 (struct
orenum
) 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