Add a proc macro for exceptions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
I would be great to have a custom derive for exceptions, similar but more powerful than create_exception. This would e.g. allow reimplementing JSONDecodeError:
#[pyexception(module = json)]
struct JSONDecodeError {
msg: String,
doc: String,
pos: usize,
lineno: Option<usize>,
colno: Option<usize>,
}
The steps required are roughly:
- Look at examples in syn and pyo3's existing proc macros to learn how to implement a proc macro. wasm-bindgen could be another useful resource.
- Implement the macro using the existing impl_exception_boilerplate and create_exception_type_object macros. The
#[proc_macro_attribute]function should go inpyo3cls/src/lib.rs, while the actual implementation should be inpyo3-derive-backend. - Write tests. Most important is one for a struct without members and one with at least 2 members.
- Add a usage example to the guide. You can just copy one of the tests for that.
If you want to work on implementing this or have any questions, feel free ask here or on gitter!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the existing proc macros and the examples in syn, then inspect pyo3cls/src/lib.rs and the pyo3-derive-backend implementation. Reuse the referenced exception macros, add tests for exceptions with and without members, and add a guide usage example. Done means the custom derive supports the requested exception shape and the tests and guide example pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100