Skip to content

Commit

Permalink
clear up #[pymethods] test
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-konovalenko committed Feb 14, 2021
1 parent 4cc06f2 commit d43c19b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,27 @@ fn panic_unsendable_child() {
test_unsendable::<UnsendableChild>().unwrap();
}

fn to_string(obj: &PyAny) -> PyResult<String> {
Ok(obj.str()?.to_str()?.to_string())
fn get_length(obj: &PyAny) -> PyResult<usize> {
let length = obj.len()?;

Ok(length)
}

#[pyclass]
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
}
}
Expand All @@ -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
"#
);
})
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn test_functions_with_function_args() {

#[cfg(not(Py_LIMITED_API))]
fn datetime_to_timestamp(dt: &PyAny) -> PyResult<i64> {
let dt: &PyDateTime = dt.extract();
let dt: &PyDateTime = dt.extract()?;
let ts: f64 = dt.call_method0("timestamp")?.extract()?;

Ok(ts as i64)
Expand Down

0 comments on commit d43c19b

Please sign in to comment.