Skip to content

Commit

Permalink
docs: Fix mismatch return value, remove redundant error propagation, …
Browse files Browse the repository at this point in the history
…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 <[email protected]>

---------

Co-authored-by: Lily Foote <[email protected]>
  • Loading branch information
2 people authored and davidhewitt committed Jul 10, 2024
1 parent 3713834 commit 380b2ac
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
9 changes: 3 additions & 6 deletions guide/src/ecosystem/async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
```

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

Expand Down Expand Up @@ -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]
Expand Down
12 changes: 4 additions & 8 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
```

Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)?)
}
```

Expand Down
3 changes: 1 addition & 2 deletions guide/src/function/signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
```

Expand Down
3 changes: 1 addition & 2 deletions guide/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
```

Expand Down
12 changes: 4 additions & 8 deletions guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
```

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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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")?)
}
}
# }
Expand Down

0 comments on commit 380b2ac

Please sign in to comment.