Skip to content

Commit

Permalink
Merge #2762
Browse files Browse the repository at this point in the history
2762: Add a nox task to rustfmt code in the guide r=birkenfeld a=birkenfeld

Also apply it. Two caveats:

1) needs nightly rustfmt to be available
2) not all reformat diffs have been applied; using best judgment for readability


Co-authored-by: Georg Brandl <[email protected]>
  • Loading branch information
bors[bot] and birkenfeld authored Nov 22, 2022
2 parents d53baed + a7a53d6 commit b485199
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 123 deletions.
4 changes: 2 additions & 2 deletions guide/pyclass_parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ more accompanying `#[pyo3(...)]` annotations, e.g.:
```rust,ignore
// Argument supplied directly to the `#[pyclass]` annotation.
#[pyclass(name = "SomeName", subclass)]
struct MyClass { }
struct MyClass {}
// Argument supplied as a separate annotation.
#[pyclass]
#[pyo3(name = "SomeName", subclass)]
struct MyClass { }
struct MyClass {}
```

[params-1]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html
Expand Down
2 changes: 1 addition & 1 deletion guide/src/building_and_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The easiest way to set the correct linker arguments is to add a [`build.rs`](htt

```rust,ignore
fn main() {
pyo3_build_config::add_extension_module_link_args();
pyo3_build_config::add_extension_module_link_args();
}
```

Expand Down
13 changes: 6 additions & 7 deletions guide/src/building_and_distribution/multiple_python_versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ This allows us to write code like the following

```rust,ignore
#[cfg(Py_3_7)]
fn function_only_supported_on_python_3_7_and_up() { }
fn function_only_supported_on_python_3_7_and_up() {}
#[cfg(not(Py_3_8))]
fn function_only_supported_before_python_3_8() { }
fn function_only_supported_before_python_3_8() {}
#[cfg(not(Py_LIMITED_API))]
fn function_incompatible_with_abi3_feature() { }
fn function_incompatible_with_abi3_feature() {}
```

The following sections first show how to add these `#[cfg]` flags to your build process, and then cover some common patterns flags in a little more detail.
Expand Down Expand Up @@ -98,11 +98,10 @@ PyO3 provides the APIs [`Python::version()`] and [`Python::version_info()`] to q
use pyo3::Python;

Python::with_gil(|py| {
// PyO3 supports Python 3.7 and up.
assert!(py.version_info() >= (3, 7));
assert!(py.version_info() >= (3, 7, 0));
// PyO3 supports Python 3.7 and up.
assert!(py.version_info() >= (3, 7));
assert!(py.version_info() >= (3, 7, 0));
});

```

[`Python::version()`]: {{#PYO3_DOCS_URL}}/pyo3/struct.Python.html#method.version
Expand Down
21 changes: 10 additions & 11 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ To define a custom Python class, add the `#[pyclass]` attribute to a Rust struct
use pyo3::prelude::*;

#[pyclass]
struct Integer{
inner: i32
struct Integer {
inner: i32,
}

// A "tuple" struct
Expand Down Expand Up @@ -166,7 +166,7 @@ struct MyClass {
num: i32,
}
Python::with_gil(|py| {
let obj = PyCell::new(py, MyClass { num: 3}).unwrap();
let obj = PyCell::new(py, MyClass { num: 3 }).unwrap();
{
let obj_ref = obj.borrow(); // Get PyRef
assert_eq!(obj_ref.num, 3);
Expand Down Expand Up @@ -204,7 +204,7 @@ fn return_myclass() -> Py<MyClass> {

let obj = return_myclass();

Python::with_gil(|py|{
Python::with_gil(|py| {
let cell = obj.as_ref(py); // Py<MyClass>::as_ref returns &PyCell<MyClass>
let obj_ref = cell.borrow(); // Get PyRef<T>
assert_eq!(obj_ref.num, 1);
Expand Down Expand Up @@ -279,7 +279,7 @@ impl SubClass {
}

fn method2(self_: PyRef<'_, Self>) -> PyResult<usize> {
let super_ = self_.as_ref(); // Get &BaseClass
let super_ = self_.as_ref(); // Get &BaseClass
super_.method().map(|x| x * self_.val2)
}
}
Expand All @@ -293,13 +293,12 @@ struct SubSubClass {
impl SubSubClass {
#[new]
fn new() -> PyClassInitializer<Self> {
PyClassInitializer::from(SubClass::new())
.add_subclass(SubSubClass{val3: 20})
PyClassInitializer::from(SubClass::new()).add_subclass(SubSubClass { val3: 20 })
}

fn method3(self_: PyRef<'_, Self>) -> PyResult<usize> {
let v = self_.val3;
let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
SubClass::method2(super_).map(|x| x * v)
}
}
Expand Down Expand Up @@ -426,7 +425,7 @@ For simple cases where a member variable is just read and written with no side e
#[pyclass]
struct MyClass {
#[pyo3(get, set)]
num: i32
num: i32,
}
```

Expand Down Expand Up @@ -937,7 +936,7 @@ You may not use enums as a base class or let enums inherit from other classes.
```rust,compile_fail
# use pyo3::prelude::*;
#[pyclass(subclass)]
enum BadBase{
enum BadBase {
Var1,
}
```
Expand All @@ -949,7 +948,7 @@ enum BadBase{
struct Base;
#[pyclass(extends=Base)]
enum BadSubclass{
enum BadSubclass {
Var1,
}
```
Expand Down
1 change: 0 additions & 1 deletion guide/src/class/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
# Ok(())
# })
# }

```

## Appendix: Writing some unsafe code
Expand Down
14 changes: 7 additions & 7 deletions guide/src/class/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ print(n)
### String representations

It can't even print an user-readable representation of itself! We can fix that by defining the
`__repr__` and `__str__` methods inside a `#[pymethods]` block. We do this by accessing the value
contained inside `Number`.
`__repr__` and `__str__` methods inside a `#[pymethods]` block. We do this by accessing the value
contained inside `Number`.

```rust
```rust
# use pyo3::prelude::*;
#
# #[pyclass]
Expand Down Expand Up @@ -114,13 +114,13 @@ impl Number {
> ```rust
> # use pyo3::prelude::*;
> #[pyclass]
> struct NotHashable { }
> struct NotHashable {}
>
> #[pymethods]
> impl NotHashable {
> #[classattr]
> const __hash__: Option<Py<PyAny>> = None;
>}
> #[classattr]
> const __hash__: Option<Py<PyAny>> = None;
> }
> ```
### Comparisons
Expand Down
4 changes: 2 additions & 2 deletions guide/src/class/protocols.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ given signatures should be interpreted as follows:
# use pyo3::prelude::*;
#
#[pyclass]
struct NotHashable { }
struct NotHashable {}

#[pymethods]
impl NotHashable {
Expand Down Expand Up @@ -229,7 +229,7 @@ Use the `#[pyclass(sequence)]` annotation to instruct PyO3 to fill the `sq_lengt
# use pyo3::prelude::*;
#
#[pyclass]
struct NoContains { }
struct NoContains {}

#[pymethods]
impl NoContains {
Expand Down
Loading

0 comments on commit b485199

Please sign in to comment.