Skip to content

Commit 766a520

Browse files
committed
Documentation enhancement
1 parent 5859039 commit 766a520

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

guide/src/class.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ struct MyClass {
1818
The above example generates implementations for `PyTypeInfo`, `PyTypeObject`
1919
and `PyClass` for `MyClass`.
2020

21-
Specifically, the following implementation is generated.
21+
Specifically, the following implementation is generated:
2222

2323
```rust
2424
use pyo3::prelude::*;
2525
use pyo3::{PyClassShell, PyTypeInfo};
2626

27+
/// Class for demonstration
2728
struct MyClass {
2829
num: i32,
2930
debug: bool,
@@ -38,7 +39,7 @@ impl PyTypeInfo for MyClass {
3839

3940
const NAME: &'static str = "MyClass";
4041
const MODULE: Option<&'static str> = None;
41-
const DESCRIPTION: &'static str = "This is a demo class";
42+
const DESCRIPTION: &'static str = "Class for demonstration";
4243
const FLAGS: usize = 0;
4344

4445
#[inline]
@@ -89,14 +90,14 @@ pyo3::inventory::collect!(MyClassGeneratedPyo3Inventory);
8990
## Get Python objects from `pyclass`
9091
You sometimes need to convert your `pyclass` into a Python object in Rust code (e.g., for testing it).
9192

92-
For getting *GIL-bounded*(i.e., with `'py` lifetime) references of `pyclass`,
93+
For getting *GIL-bounded* (i.e., with `'py` lifetime) references of `pyclass`,
9394
you can use `PyClassShell<T>`.
9495
Or you can use `Py<T>` directly, for *not-GIL-bounded* references.
9596

9697
### `PyClassShell`
9798
`PyClassShell` represents the actual layout of `pyclass` on the Python heap.
9899

99-
If you want to instantiate `pyclass` in Python and get the the reference,
100+
If you want to instantiate `pyclass` in Python and get the reference,
100101
you can use `PyClassShell::new_ref` or `PyClassShell::new_mut`.
101102

102103
```rust
@@ -182,21 +183,24 @@ impl MyClass {
182183
}
183184
```
184185

185-
Rules for the `new` method:
186-
187-
* If no method marked with `#[new]` is declared, object instances can only be created
188-
from Rust, but not from Python.
189-
* All parameters are from Python.
190-
* It can return one of these types:
191-
- `T`
192-
- `PyResult<T>`
193-
- `PyClassInitializer<T>`
194-
- `PyResult<PyClassInitializer<T>>`
195-
* If you pyclass declared with `#[pyclass(extends=BaseType)]` and `BaseType`
196-
is also `#[pyclass]`, you have to return `PyClassInitializer<T>` or
197-
`PyResult<PyClassInitializer<T>>` with the baseclass initialized. See the
198-
below Inheritance section for detail.
199-
* For details on the parameter list, see the `Method arguments` section below.
186+
If no method marked with `#[new]` is declared, object instances can only be
187+
created from Rust, but not from Python.
188+
189+
For arguments, see the `Method arguments` section below.
190+
191+
### Return type
192+
193+
If your pyclass is declared with baseclass(i.e., you use `#[pyclass(extends=...)])`),
194+
you must return a `PyClassInitializer` with the base class initialized.
195+
196+
For constructors that may fail, you should wrap the return type in a PyResult as well.
197+
Consult the table below to determine which type your constructor should return:
198+
199+
200+
| | **Cannot fail** | **May fail** |
201+
|--------------------|-------------------------|-----------------------------------|
202+
| **No inheritance** | `T` | `PyResult<T>` |
203+
| **Inheritance** | `PyClassInitializer<T>` | `PyResult<PyClassInitializer<T>>` |
200204

201205
## Inheritance
202206

src/pyclass.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) unsafe fn default_alloc<T: PyTypeInfo>() -> *mut ffi::PyObject {
2626
alloc(tp_ptr, 0)
2727
}
2828

29-
/// A trait that enables custome alloc/dealloc implementations for pyclasses.
29+
/// A trait that enables custom alloc/dealloc implementations for pyclasses.
3030
pub trait PyClassAlloc: PyTypeInfo + Sized {
3131
unsafe fn alloc(_py: Python) -> *mut Self::ConcreteLayout {
3232
default_alloc::<Self>() as _
@@ -65,7 +65,7 @@ pub unsafe fn tp_free_fallback(obj: *mut ffi::PyObject) {
6565
/// If `PyClass` is implemented for `T`, then we can use `T` in the Python world,
6666
/// via `PyClassShell`.
6767
///
68-
/// `#[pyclass]` attribute automatically implement this trait for your Rust struct,
68+
/// The `#[pyclass]` attribute automatically implements this trait for your Rust struct,
6969
/// so you don't have to use this trait directly.
7070
pub trait PyClass:
7171
PyTypeInfo<ConcreteLayout = PyClassShell<Self>> + Sized + PyClassAlloc + PyMethodsProtocol
@@ -96,9 +96,10 @@ where
9696
}
9797
}
9898

99-
/// `PyClassShell` represents the concrete layout of `PyClass` in the Python heap.
99+
/// `PyClassShell` represents the concrete layout of `T: PyClass` when it is converted
100+
/// to a Python class.
100101
///
101-
/// You can use it for testing your `#[pyclass]` correctly works.
102+
/// You can use it to test your `#[pyclass]` correctly works.
102103
///
103104
/// ```
104105
/// # use pyo3::prelude::*;
@@ -268,7 +269,7 @@ where
268269
}
269270
}
270271

271-
/// A speciall initializer for `PyClassShell<T>`, which enables `super().__init__`
272+
/// A special initializer for `PyClassShell<T>`, which enables `super().__init__`
272273
/// in Rust code.
273274
///
274275
/// You have to use it only when your `#[pyclass]` extends another `#[pyclass]`.
@@ -387,9 +388,9 @@ impl<T: PyTypeInfo> PyClassInitializer<T> {
387388
}
388389
}
389390

390-
/// Represets that we can convert the type to `PyClassInitializer`.
391+
/// Represents that we can convert the type to `PyClassInitializer`.
391392
///
392-
/// It is mainly used in our proc-macro code.
393+
/// This is mainly used in our proc-macro code.
393394
pub trait IntoInitializer<T: PyClass> {
394395
fn into_initializer(self) -> PyResult<PyClassInitializer<T>>;
395396
}
@@ -418,7 +419,7 @@ impl<T: PyClass> IntoInitializer<T> for PyResult<PyClassInitializer<T>> {
418419
}
419420
}
420421

421-
/// Register new type in python object system.
422+
/// Register new type in the python object system.
422423
#[cfg(not(Py_LIMITED_API))]
423424
pub fn initialize_type<T>(py: Python, module_name: Option<&str>) -> PyResult<*mut ffi::PyTypeObject>
424425
where

0 commit comments

Comments
 (0)