You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been through the docs and guide a few times and I'm stuck figuring out how to go about this.
I have a library that should be usable in both Rust and Python, where Python support is feature-gated. The Rust library defines a bunch of error types as enums using thiserror, e.g.
What I'm struggling to wrap my head around is the best way to transform this error type into a Python exception.
pyclass fails because it is an enum and not a struct.
create_exception! creates a new type and does not modify an existing one.
impl_exception_boilerplate! seems closest, but conflicts with the implementation of std::error::Error from thiserror. If I modify the code to remove the use of thiserror (i.e. impl things myself), I get a new error that SomeError does not impl PyTypeInfo, which I am not comfortable trying to implement on my own.
Is there something I'm missing, or is this something that just isn't every doable in the current state of PyO3?
The text was updated successfully, but these errors were encountered:
If you want to use builtin Python exception types and be able to return Result<_, SomeError> from #[pymethods], impl From<SomeError> for PyErr might suit what you need?
If you want a custom exception type I would probably recommend #[pyclass(extends=PyException)] to make a separate error classe which the From impl would target.
I've been through the docs and guide a few times and I'm stuck figuring out how to go about this.
I have a library that should be usable in both Rust and Python, where Python support is feature-gated. The Rust library defines a bunch of error types as enums using
thiserror
, e.g.What I'm struggling to wrap my head around is the best way to transform this error type into a Python exception.
pyclass
fails because it is an enum and not a struct.create_exception!
creates a new type and does not modify an existing one.impl_exception_boilerplate!
seems closest, but conflicts with the implementation ofstd::error::Error
fromthiserror
. If I modify the code to remove the use ofthiserror
(i.e. impl things myself), I get a new error thatSomeError
does notimpl PyTypeInfo
, which I am not comfortable trying to implement on my own.Is there something I'm missing, or is this something that just isn't every doable in the current state of PyO3?
The text was updated successfully, but these errors were encountered: