PyO3 / PyO3/rust-numpy

Creating getters and setters for `ndarray` fields of structs

Open
#465 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.4k
Forks
141
Avg merge
16m
Merged PRs (30d)
3

Description

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)]
pub struct Foo {
    vec: Array2<f64>,  
}

#[pymodule]
fn my_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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Rust example against rust-numpy v0.22.0, then read the linked discussions 357 and 393 and compare the PyO3 smallvec conversion implementation. Done would mean a documented, agreed approach for automatically exposing ndarray struct fields through getters and setters, including the required conversion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python, rust
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.