Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot extract Rust arrray from NumPy array #2674

Closed
adamreichold opened this issue Oct 11, 2022 · 0 comments · Fixed by #2675
Closed

Cannot extract Rust arrray from NumPy array #2674

adamreichold opened this issue Oct 11, 2022 · 0 comments · Fixed by #2675

Comments

@adamreichold
Copy link
Member

I am getting TypeError: argument 'oned': 'ndarray' object cannot be converted to 'Sequence' running test.py with the following files:

src/lib.rs

use pyo3::prelude::*;

#[pyclass(module = "rust_module")]
#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct DummyClass{
    name: String,
    oned: [f64;3],
    twod: [[f64;3];3],
}

#[pymethods]
impl DummyClass {
    #[new]
    pub fn pynew(
        name: String,
        oned: [f64;3],
        twod: [[f64;3];3],
    ) -> PyResult<Self>{
        Ok(Self { name, oned, twod})
    }

    fn __call__(&self) -> PyResult<String>{
        Ok("Success".into())
    }
}

#[pymodule]
fn pyo3_pure(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<DummyClass>()?;
    Ok(())
}

Cargo.toml

[package]
name = "reproduce-extract"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.17.2", features = ["serde", "abi3-py37", "extension-module"] }
numpy = { version = "^0.17.2" }
serde = { version = "^1.0", features = ["derive"]}

[lib]
name = "pyo3_pure"
crate-type = ["cdylib"]

pyproject.toml

[build-system]
requires = ["maturin>=0.13,<0.14"]
build-backend = "maturin"

[tool.maturin]
bindings = "pyo3"

[project]
# The name pyo3_pure instead of pyo3-pure is intentional,
# it's used as a crate name resolution regression test
name = "pyo3_pure"
version = "0.1.0"

dependencies = [
    "numpy"
]

test.py

from pyo3_pure import DummyClass
import numpy as np

# test = DummyClass("hello",[0,0,0],[[0,0,0],[0,0,0],[0,0,0]])
test = DummyClass("hello",np.array([0,0,0]),np.array([[0,0,0],[0,0,0],[0,0,0]]))

print(test())

Originally posted by @Billyangnz in #2615 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant