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 tried to write Python bindings for a struct containing ndarray type fields, but it does not compile: (rust-numpy v0.22.0)
use pyo3::prelude::*;use pyo3::{pymodule,Bound,PyResult};use numpy::ndarray::Array2;#[pyclass(get_all, set_all)]#[derive(Debug,Clone)]pubstructFoo{vec:Array2<f64>,}#[pymodule]fnmy_module(m:&Bound<'_,PyModule>) -> PyResult<()>{
m.add_class::<Foo>()?;Ok(())}
I read the existing discussions (this and this) and they suggests writing getters and setters manually for each field, which is unsatisfactory.
This currently greatly limits the ability to make existing APIs that contain ndarrays available to Python: We can only use free functions but not structs or classmethods.
Why is this limitation ? Could it be solved by simply implementing the IntoPy<Py<PyAny>> and FromPyObject traits for ndarray ?
I see that this is done for smallvec's and having smallvec's as fields just works as expected.
Note also that this feature is present in C++'s pybind11 (the getters return a read-only array).
I would greatly appreciate the ability to bind ndarray fields and automatically provide getters and setters in Python. This would allow to fully bind an existing Rust API to Python.
Thanks a lot!
The text was updated successfully, but these errors were encountered:
Because rust-numpy is a separate crate to PyO3, we can't directly implement for ndarray types due to the orphan rule. You could probably use a newtype in your struct e.g. (struct WrappedArray(Array2<f64>)) and implement IntoPy and FromPyObject yourself.
It would be nice to have a cleaner solution for this in the future, though note that FromPyObject can be #[derive]-d and we will be adding #[derive(IntoPyObject)] (a new trait) in PyO3 0.23, which will make the newtype pretty easy.
Thanks for the reply ! Then I'll use the workaround you mentioned, if it's currently not supported. I would be happy to see this supported in the future in the otherwise great Python-bindings library PyO3 :)
I tried to write Python bindings for a struct containing
ndarray
type fields, but it does not compile: (rust-numpy
v0.22.0)I read the existing discussions (this and this) and they suggests writing getters and setters manually for each field, which is unsatisfactory.
This currently greatly limits the ability to make existing APIs that contain ndarrays available to Python: We can only use free functions but not structs or classmethods.
Why is this limitation ? Could it be solved by simply implementing the
IntoPy<Py<PyAny>>
andFromPyObject
traits forndarray
?I see that this is done for
smallvec
's and havingsmallvec
's as fields just works as expected.Note also that this feature is present in C++'s
pybind11
(the getters return a read-only array).I would greatly appreciate the ability to bind
ndarray
fields and automatically provide getters and setters in Python. This would allow to fully bind an existing Rust API to Python.Thanks a lot!
The text was updated successfully, but these errors were encountered: