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 <code@lilyf.org>

---------

Co-authored-by: Lily Foote <code@lilyf.org>
This commit is contained in:
Sede Soukossi 2024-07-08 22:30:44 +02:00 committed by GitHub
parent 3c155d9fef
commit 1861d6d379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 26 deletions

View File

@ -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]

View File

@ -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<usize> {
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)?)
}
```

View File

@ -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)?)
}
```

View File

@ -159,8 +159,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
/// 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)?)
}
```

View File

@ -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")?)
}
}
# }