How to use RUST two "nested" structs in Python

0

Hi,I have this problem. I created two stuctures in RUST where one contains required fields and the other contains optional fields. The compilation went fine, in Python I imported the library but when I want to create an object in Python, that will contain optional fields I get an error.

#[pyclass(name = "class_1")]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PyClassMandatory {
    #[pyo3(get, set)]
    field1: String,
    #[pyo3(get, set)]
    field2: String,
    #[pyo3(get, set)]
    py_class_non_mandatory: PyClassNonMandatory
}


#[pyclass(name = "class_1")]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PyClassNonMandatory {
    #[pyo3(get, set)]
    field3: Option<String>,
    #[pyo3(get, set)]
    field4: Option<String>
}

impl Default for PyClassNonMandatory {
    fn default() -> PyClassNonMandatory {
        PyClassNonMandatory {
            field3: Default::default(),
            field4: Default::default()
        }
    }
}

#[pymethods]
impl PyClassMandatory {
    #[new]
    fn __new__(field1: String, field2: String) -> Self {
        PyClassMandatory {
        field1,
        field2,
        py_class_non_mandatory: Default::default()
    }
}
1

What kind of error?

5

@Pewex chyba przypadkowo włączył tłumacza, bo resztę postów ma po polsku :]

0
enedil napisał(a):

What kind of error?

In Python

After library is loaded:
>>>> import test_lib

I've tried to create an object:
>>> test_object = test_lib.class_1(field1="aaa", field2= "bbb", field3="ccc")

And I got an error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: class_1.__new__() got an unexpected keyword argument 'field3'
>>>

1 użytkowników online, w tym zalogowanych: 0, gości: 1