I believe a lot has changed in pyo3
since the last answer. Also I want to give a complete example including the return value and the acquisition of the GIL.
#[pyfunction]
pub fn get_heterogeneous_python_dict() -> PyResult<PyObject> {
Python::with_gil(|py| {
let d = PyDict::new(py);
d.set_item("str", "asd").unwrap();
d.set_item("num", 8).unwrap();
Ok(d.into())
})
}
Proving on the Python side the heterogeneous types
def test_get_heterogeneous_python_dict(self):
d = get_heterogeneous_python_dict()
self.assertEqual(d["num"], 8)
self.assertEqual(d["str"], "asd")
print(d)