Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Creating getters and setters for `ndarray` fields of structs

Aperta
#465 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
numpy, python, rust

Direzione di ricerca

Inizia riproducendo l’esempio Rust con rust-numpy v0.22.0, quindi leggi le discussioni collegate 357 e 393 e confronta l’implementazione della conversione di smallvec di PyO3. Il lavoro sarà completo quando sarà disponibile un approccio documentato e concordato per esporre automaticamente i campi delle struct ndarray tramite getter e setter, incluso il comportamento di conversione richiesto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
Rust
Stelle
1.4k
Fork
141
Merge medio
16m
PR unite (30g)
3

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PyO3/rust-numpy

Tutte le issue di PyO3/rust-numpy

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.