From 1861d6d379d8f04bf94e529ddc329ab831a60d2d Mon Sep 17 00:00:00 2001 From: Sede Soukossi <4968379+styvane@users.noreply.github.com> Date: Mon, 8 Jul 2024 22:30:44 +0200 Subject: [PATCH] docs: Fix mismatch return value, remove redundant error propagation, and additional fixes (#4318) * Fix mismatch return value, remove redundant error propagation, and fix typo * Update guide/src/function/signature.md Co-authored-by: Lily Foote --------- Co-authored-by: Lily Foote --- guide/src/ecosystem/async-await.md | 9 +++------ guide/src/function.md | 12 ++++-------- guide/src/function/signature.md | 3 +-- guide/src/getting-started.md | 3 +-- guide/src/module.md | 12 ++++-------- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/guide/src/ecosystem/async-await.md b/guide/src/ecosystem/async-await.md index 9c0ad19bdef..0128efbc8a3 100644 --- a/guide/src/ecosystem/async-await.md +++ b/guide/src/ecosystem/async-await.md @@ -129,9 +129,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> { #[pymodule] fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; - - Ok(()) + m.add_function(wrap_pyfunction!(rust_sleep, m)?) } ``` @@ -152,8 +150,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>>> { #[pymodule] fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(rust_sleep, m)?) } ``` @@ -257,7 +254,7 @@ async def py_sleep(): await_coro(py_sleep()) ``` -If for you wanted to pass a callable function to the `#[pyfunction]` instead, (i.e. the last line becomes `await_coro(py_sleep))`, then the above example needs to be tweaked to first call the callable to get the coroutine: +If you wanted to pass a callable function to the `#[pyfunction]` instead, (i.e. the last line becomes `await_coro(py_sleep))`, then the above example needs to be tweaked to first call the callable to get the coroutine: ```rust #[pyfunction] diff --git a/guide/src/function.md b/guide/src/function.md index 86ac4c89b46..5e6cfaba773 100644 --- a/guide/src/function.md +++ b/guide/src/function.md @@ -14,8 +14,7 @@ fn double(x: usize) -> usize { #[pymodule] fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(double, m)?) } ``` @@ -56,8 +55,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python #[pymodule] fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(no_args_py, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(no_args_py, m)?) } # Python::with_gil(|py| { @@ -113,8 +111,7 @@ The `#[pyo3]` attribute can be used on individual arguments to modify properties use pyo3::prelude::*; fn get_length(obj: &Bound<'_, PyAny>) -> PyResult { - let length = obj.len()?; - Ok(length) + obj.len() } #[pyfunction] @@ -204,8 +201,7 @@ fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { x * 2 } - m.add_function(wrap_pyfunction!(double, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(double, m)?) } ``` diff --git a/guide/src/function/signature.md b/guide/src/function/signature.md index 69949220be6..a9be5983422 100644 --- a/guide/src/function/signature.md +++ b/guide/src/function/signature.md @@ -22,8 +22,7 @@ fn num_kwds(kwds: Option<&Bound<'_, PyDict>>) -> usize { #[pymodule] fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(num_kwds, m)?).unwrap(); - Ok(()) + m.add_function(wrap_pyfunction!(num_kwds, m)?) } ``` diff --git a/guide/src/getting-started.md b/guide/src/getting-started.md index ede48d50c33..e2cc040bbd7 100644 --- a/guide/src/getting-started.md +++ b/guide/src/getting-started.md @@ -159,8 +159,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult { /// import the module. #[pymodule] fn pyo3_example(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(sum_as_string, m)?) } ``` diff --git a/guide/src/module.md b/guide/src/module.md index 2c4039a6e76..a2cb8b37a05 100644 --- a/guide/src/module.md +++ b/guide/src/module.md @@ -13,8 +13,7 @@ fn double(x: usize) -> usize { /// This module is implemented in Rust. #[pymodule] fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(double, m)?) } ``` @@ -35,8 +34,7 @@ fn double(x: usize) -> usize { #[pymodule] #[pyo3(name = "custom_name")] fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; - Ok(()) + m.add_function(wrap_pyfunction!(double, m)?) } ``` @@ -80,8 +78,7 @@ fn parent_module(m: &Bound<'_, PyModule>) -> PyResult<()> { fn register_child_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { let child_module = PyModule::new_bound(parent_module.py(), "child_module")?; child_module.add_function(wrap_pyfunction!(func, &child_module)?)?; - parent_module.add_submodule(&child_module)?; - Ok(()) + parent_module.add_submodule(&child_module) } #[pyfunction] @@ -143,8 +140,7 @@ mod my_extension { #[pymodule_init] fn init(m: &Bound<'_, PyModule>) -> PyResult<()> { // Arbitrary code to run at the module initialization - m.add("double2", m.getattr("double")?)?; - Ok(()) + m.add("double2", m.getattr("double")?) } } # }