diff --git a/tests/test_class_basics.rs b/tests/test_class_basics.rs index 910d1eb5899..5f63ccefa82 100644 --- a/tests/test_class_basics.rs +++ b/tests/test_class_basics.rs @@ -245,8 +245,10 @@ fn panic_unsendable_child() { test_unsendable::().unwrap(); } -fn to_string(obj: &PyAny) -> PyResult { - Ok(obj.str()?.to_str()?.to_string()) +fn get_length(obj: &PyAny) -> PyResult { + let length = obj.len()?; + + Ok(length) } #[pyclass] @@ -254,16 +256,16 @@ struct ClassWithFromPyWithMethods {} #[pymethods] impl ClassWithFromPyWithMethods { - fn instance_method(&self, #[pyo3(from_py_with = "to_string")] argument: String) -> String { + fn instance_method(&self, #[pyo3(from_py_with = "get_length")] argument: usize) -> usize { argument } #[classmethod] - fn classmethod(_cls: &PyType, #[pyo3(from_py_with = "to_string")] argument: String) -> String { + fn classmethod(_cls: &PyType, #[pyo3(from_py_with = "get_length")] argument: usize) -> usize { argument } #[staticmethod] - fn staticmethod(#[pyo3(from_py_with = "to_string")] argument: String) -> String { + fn staticmethod(#[pyo3(from_py_with = "get_length")] argument: usize) -> usize { argument } } @@ -277,11 +279,11 @@ fn test_pymethods_from_py_with() { py, instance, r#" - arg = {1: 1} + arg = {1: 1, 2: 3} - assert instance.instance_method(arg) == '{1: 1}' - assert instance.classmethod(arg) == '{1: 1}' - assert instance.staticmethod(arg) == '{1: 1}' + assert instance.instance_method(arg) == 2 + assert instance.classmethod(arg) == 2 + assert instance.staticmethod(arg) == 2 "# ); }) diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 5602b037584..b59e6e932c4 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -113,7 +113,7 @@ fn test_functions_with_function_args() { #[cfg(not(Py_LIMITED_API))] fn datetime_to_timestamp(dt: &PyAny) -> PyResult { - let dt: &PyDateTime = dt.extract(); + let dt: &PyDateTime = dt.extract()?; let ts: f64 = dt.call_method0("timestamp")?.extract()?; Ok(ts as i64)