Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Fix mismatch return value, remove redundant error propagation, and additional fixes #4318

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading