Skip to content

Commit 1486fe6

Browse files
committed
Use single-arg form for #[pymodule] functions in docs and tests
1 parent a15e4b1 commit 1486fe6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+85
-85
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
8686
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
8787
/// import the module.
8888
#[pymodule]
89-
fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
89+
fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> {
9090
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
9191
Ok(())
9292
}

examples/decorator/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl PyCounter {
6060
}
6161

6262
#[pymodule]
63-
pub fn decorator(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
63+
pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> {
6464
module.add_class::<PyCounter>()?;
6565
Ok(())
6666
}

examples/getitem/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl ExampleContainer {
7575

7676
#[pymodule]
7777
#[pyo3(name = "getitem")]
78-
fn example(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
78+
fn example(m: &Bound<'_, PyModule>) -> PyResult<()> {
7979
// ? -https://github.com/PyO3/maturin/issues/475
8080
m.add_class::<ExampleContainer>()?;
8181
Ok(())

examples/maturin-starter/src/submodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl SubmoduleClass {
1616
}
1717

1818
#[pymodule]
19-
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
19+
pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
2020
m.add_class::<SubmoduleClass>()?;
2121
Ok(())
2222
}

examples/plugin/plugin_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Gadget {
2626

2727
/// A Python module for plugin interface types
2828
#[pymodule]
29-
pub fn plugin_api(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
29+
pub fn plugin_api(m: &Bound<'_, PyModule>) -> PyResult<()> {
3030
m.add_class::<Gadget>()?;
3131
Ok(())
3232
}

examples/setuptools-rust-starter/src/submodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl SubmoduleClass {
1616
}
1717

1818
#[pymodule]
19-
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
19+
pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
2020
m.add_class::<SubmoduleClass>()?;
2121
Ok(())
2222
}

examples/word-count/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn count_line(line: &str, needle: &str) -> usize {
3333
}
3434

3535
#[pymodule]
36-
fn word_count(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
36+
fn word_count(m: &Bound<'_, PyModule>) -> PyResult<()> {
3737
m.add_function(wrap_pyfunction!(search, m)?)?;
3838
m.add_function(wrap_pyfunction!(search_sequential, m)?)?;
3939
m.add_function(wrap_pyfunction!(search_sequential_allow_threads, m)?)?;

guide/src/class.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ The next step is to create the module initializer and add our class to it:
181181
# struct Number(i32);
182182
#
183183
#[pymodule]
184-
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
184+
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
185185
m.add_class::<Number>()?;
186186
Ok(())
187187
}

guide/src/class/numeric.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl Number {
326326
}
327327

328328
#[pymodule]
329-
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
329+
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
330330
m.add_class::<Number>()?;
331331
Ok(())
332332
}

guide/src/class/object.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Number {
1818
}
1919

2020
#[pymodule]
21-
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
21+
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
2222
m.add_class::<Number>()?;
2323
Ok(())
2424
}
@@ -295,7 +295,7 @@ impl Number {
295295
}
296296

297297
#[pymodule]
298-
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
298+
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
299299
m.add_class::<Number>()?;
300300
Ok(())
301301
}

guide/src/ecosystem/async-await.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
128128
}
129129

130130
#[pymodule]
131-
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
131+
fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
132132
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
133133

134134
Ok(())
@@ -151,7 +151,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
151151
}
152152

153153
#[pymodule]
154-
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
154+
fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
155155
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
156156
Ok(())
157157
}
@@ -475,7 +475,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
475475
}
476476

477477
#[pymodule]
478-
fn my_async_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
478+
fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
479479
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
480480

481481
Ok(())

guide/src/ecosystem/logging.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ fn log_something() {
3030
}
3131

3232
#[pymodule]
33-
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
33+
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
3434
// A good place to install the Rust -> Python logger.
3535
pyo3_log::init();
3636

37-
m.add_function(wrap_pyfunction!(log_something))?;
37+
m.add_function(wrap_pyfunction!(log_something, m)?)?;
3838
Ok(())
3939
}
4040
```

guide/src/function.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn double(x: usize) -> usize {
1313
}
1414

1515
#[pymodule]
16-
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
16+
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
1717
m.add_function(wrap_pyfunction!(double, m)?)?;
1818
Ok(())
1919
}
@@ -55,7 +55,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
5555
}
5656

5757
#[pymodule]
58-
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> {
58+
fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
5959
m.add_function(wrap_pyfunction!(no_args_py, m)?)?;
6060
Ok(())
6161
}
@@ -92,7 +92,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
9292
}
9393

9494
#[pymodule]
95-
fn module_with_fn(py: Python<'_>, m: &PyModule) -> PyResult<()> {
95+
fn module_with_fn(m: &Bound<'_, PyModule>) -> PyResult<()> {
9696
m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)
9797
}
9898
```
@@ -166,7 +166,7 @@ Python argument passing convention.) It then embeds the call to the Rust functio
166166
FFI-wrapper function. This wrapper handles extraction of the regular arguments and the keyword
167167
arguments from the input `PyObject`s.
168168

169-
The `wrap_pyfunction` macro can be used to directly get a `PyCFunction` given a
169+
The `wrap_pyfunction` macro can be used to directly get a `Bound<PyCFunction>` given a
170170
`#[pyfunction]` and a `PyModule`: `wrap_pyfunction!(rust_fun, module)`.
171171

172172
## `#[pyfn]` shorthand
@@ -197,7 +197,7 @@ documented in the rest of this chapter. The code above is expanded to the follow
197197
use pyo3::prelude::*;
198198

199199
#[pymodule]
200-
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
200+
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
201201
#[pyfunction]
202202
fn double(x: usize) -> usize {
203203
x * 2

guide/src/function/signature.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn num_kwds(kwds: Option<&PyDict>) -> usize {
2121
}
2222

2323
#[pymodule]
24-
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> {
24+
fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
2525
m.add_function(wrap_pyfunction!(num_kwds, m)?).unwrap();
2626
Ok(())
2727
}

guide/src/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
163163
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
164164
/// import the module.
165165
#[pymodule]
166-
fn pyo3_example(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
166+
fn pyo3_example(m: &Bound<'_, PyModule>) -> PyResult<()> {
167167
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
168168
Ok(())
169169
}

guide/src/module.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn double(x: usize) -> usize {
1212

1313
/// This module is implemented in Rust.
1414
#[pymodule]
15-
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
15+
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
1616
m.add_function(wrap_pyfunction!(double, m)?)?;
1717
Ok(())
1818
}
@@ -34,7 +34,7 @@ fn double(x: usize) -> usize {
3434

3535
#[pymodule]
3636
#[pyo3(name = "custom_name")]
37-
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
37+
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
3838
m.add_function(wrap_pyfunction!(double, m)?)?;
3939
Ok(())
4040
}

guide/src/python_from_rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn add_one(x: i64) -> i64 {
283283
}
284284

285285
#[pymodule]
286-
fn foo(_py: Python<'_>, foo_module: &PyModule) -> PyResult<()> {
286+
fn foo(foo_module: &Bound<'_, PyModule>) -> PyResult<()> {
287287
foo_module.add_function(wrap_pyfunction!(add_one, foo_module)?)?;
288288
Ok(())
289289
}

guide/src/trait_bounds.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct UserModel {
132132
}
133133

134134
#[pymodule]
135-
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
135+
fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> {
136136
m.add_class::<UserModel>()?;
137137
Ok(())
138138
}
@@ -489,7 +489,7 @@ pub struct UserModel {
489489
}
490490

491491
#[pymodule]
492-
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
492+
fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> {
493493
m.add_class::<UserModel>()?;
494494
m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?;
495495
Ok(())

pytests/src/awaitable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl FutureAwaitable {
7979
}
8080

8181
#[pymodule]
82-
pub fn awaitable(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
82+
pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> {
8383
m.add_class::<IterAwaitable>()?;
8484
m.add_class::<FutureAwaitable>()?;
8585
Ok(())

pytests/src/buf_and_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn return_memoryview(py: Python<'_>) -> PyResult<Bound<'_, PyMemoryView>> {
4848
}
4949

5050
#[pymodule]
51-
pub fn buf_and_str(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
51+
pub fn buf_and_str(m: &Bound<'_, PyModule>) -> PyResult<()> {
5252
m.add_class::<BytesExtractor>()?;
5353
m.add_function(wrap_pyfunction!(return_memoryview, m)?)?;
5454
Ok(())

pytests/src/comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl OrderedDefaultNe {
101101
}
102102

103103
#[pymodule]
104-
pub fn comparisons(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
104+
pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> {
105105
m.add_class::<Eq>()?;
106106
m.add_class::<EqDefaultNe>()?;
107107
m.add_class::<Ordered>()?;

pytests/src/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl TzClass {
205205
}
206206

207207
#[pymodule]
208-
pub fn datetime(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
208+
pub fn datetime(m: &Bound<'_, PyModule>) -> PyResult<()> {
209209
m.add_function(wrap_pyfunction!(make_date, m)?)?;
210210
m.add_function(wrap_pyfunction!(get_date_tuple, m)?)?;
211211
m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?;

pytests/src/dict_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
use pyo3::types::PyDict;
44

55
#[pymodule]
6-
pub fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
6+
pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> {
77
m.add_class::<DictSize>()?;
88
Ok(())
99
}

pytests/src/enums.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use pyo3::{pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction, PyResult, Python};
1+
use pyo3::{pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction, Bound, PyResult};
22

33
#[pymodule]
4-
pub fn enums(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
4+
pub fn enums(m: &Bound<'_, PyModule>) -> PyResult<()> {
55
m.add_class::<SimpleEnum>()?;
66
m.add_class::<ComplexEnum>()?;
77
m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?;

pytests/src/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny>
3131
}
3232

3333
#[pymodule]
34-
pub fn misc(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
34+
pub fn misc(m: &Bound<'_, PyModule>) -> PyResult<()> {
3535
m.add_function(wrap_pyfunction!(issue_219, m)?)?;
3636
m.add_function(wrap_pyfunction!(get_type_full_name, m)?)?;
3737
m.add_function(wrap_pyfunction!(accepts_bool, m)?)?;

pytests/src/objstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ impl ObjStore {
1919
}
2020

2121
#[pymodule]
22-
pub fn objstore(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
22+
pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> {
2323
m.add_class::<ObjStore>()
2424
}

pytests/src/othermod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn double(x: i32) -> i32 {
2929
}
3030

3131
#[pymodule]
32-
pub fn othermod(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
32+
pub fn othermod(m: &Bound<'_, PyModule>) -> PyResult<()> {
3333
m.add_function(wrap_pyfunction!(double, m)?)?;
3434

3535
m.add_class::<ModClass>()?;

pytests/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn take_pathbuf(path: PathBuf) -> PathBuf {
1212
}
1313

1414
#[pymodule]
15-
pub fn path(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
15+
pub fn path(m: &Bound<'_, PyModule>) -> PyResult<()> {
1616
m.add_function(wrap_pyfunction!(make_path, m)?)?;
1717
m.add_function(wrap_pyfunction!(take_pathbuf, m)?)?;
1818

pytests/src/pyclasses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl AssertingBaseClassGilRef {
8080
struct ClassWithoutConstructor;
8181

8282
#[pymodule]
83-
pub fn pyclasses(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
83+
pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> {
8484
m.add_class::<EmptyClass>()?;
8585
m.add_class::<PyClassIter>()?;
8686
m.add_class::<AssertingBaseClass>()?;

pytests/src/pyfunctions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn args_kwargs<'a>(
6464
}
6565

6666
#[pymodule]
67-
pub fn pyfunctions(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
67+
pub fn pyfunctions(m: &Bound<'_, PyModule>) -> PyResult<()> {
6868
m.add_function(wrap_pyfunction!(none, m)?)?;
6969
m.add_function(wrap_pyfunction!(simple, m)?)?;
7070
m.add_function(wrap_pyfunction!(simple_args, m)?)?;

pytests/src/sequence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn vec_to_vec_pystring(vec: Vec<&PyString>) -> Vec<&PyString> {
1717
}
1818

1919
#[pymodule]
20-
pub fn sequence(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
20+
pub fn sequence(m: &Bound<'_, PyModule>) -> PyResult<()> {
2121
m.add_function(wrap_pyfunction!(vec_to_vec_i32, m)?)?;
2222
m.add_function(wrap_pyfunction!(array_to_array_i32, m)?)?;
2323
m.add_function(wrap_pyfunction!(vec_to_vec_pystring, m)?)?;

pytests/src/subclassing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Subclassable {
1818
}
1919

2020
#[pymodule]
21-
pub fn subclassing(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
21+
pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2222
m.add_class::<Subclassable>()?;
2323
Ok(())
2424
}

src/conversions/indexmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
//! }
7272
//!
7373
//! #[pymodule]
74-
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
74+
//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
7575
//! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?;
7676
//! Ok(())
7777
//! }

src/conversions/num_bigint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! }
3232
//!
3333
//! #[pymodule]
34-
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
34+
//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
3535
//! m.add_function(wrap_pyfunction!(add_one, m)?)?;
3636
//! Ok(())
3737
//! }

0 commit comments

Comments
 (0)