You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, `object`, i.e. `PyAny` is used as the base class.
412
411
To override this default, use the `extends` parameter for `pyclass` with the full path to the base class.
413
412
Currently, only classes defined in Rust and builtins provided by PyO3 can be inherited from; inheriting from other classes defined in Python is not yet supported ([#991](https://github.com/PyO3/pyo3/issues/991)).
414
413
415
-
For convenience, `(T, U)` implements `Into<PyClassInitializer<T>>` where `U` is the base class of `T`.
416
-
But for a more deeply nested inheritance, you have to return `PyClassInitializer<T>` explicitly.
414
+
To initialize a class, which inherits from another class, use the `PyClassInitializer` API.
417
415
418
416
To get a parent class from a child, use [`PyRef`] instead of `&self` for methods, or [`PyRefMut`] instead of `&mut self`.
419
417
Then you can access a parent class by `self_.as_super()` as `&PyRef<Self::BaseClass>`, or by `self_.into_super()` as `PyRef<Self::BaseClass>` (and similar for the `PyRefMut` case).
Copy file name to clipboardExpand all lines: guide/src/migration.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,6 +88,52 @@ let obj_2 = existing_bound.clone();
88
88
# })
89
89
```
90
90
91
+
### Deprecation of super class initialization from tuples
92
+
93
+
Performing superclass initialitation from a subclass via a tuple is deprecated and will be removed in a future PyO3 version.
94
+
This also includes the `From<(S, B)> for PyClassInitializer<S>` impl which we can't warn against.
95
+
To migrate use `PyClassInitializer` directly:
96
+
97
+
Before:
98
+
99
+
```rust
100
+
# #![allow(deprecated)]
101
+
# usepyo3::prelude::*;
102
+
#[pyclass(subclass)]
103
+
structBase;
104
+
105
+
#[pyclass(extends=Base)]
106
+
structSub;
107
+
108
+
#[pymethods]
109
+
implSub {
110
+
#[new]
111
+
fnnew() -> (Self, Base) {
112
+
(Self, Base)
113
+
}
114
+
}
115
+
```
116
+
117
+
After:
118
+
119
+
```rust
120
+
# usepyo3::prelude::*;
121
+
#[pyclass(subclass)]
122
+
structBase;
123
+
124
+
#[pyclass(extends=Base)]
125
+
structSub;
126
+
127
+
#[pymethods]
128
+
implSub {
129
+
#[new]
130
+
fnnew() ->PyClassInitializer<Self> {
131
+
PyClassInitializer::from(Base)
132
+
.add_subclass(Self)
133
+
}
134
+
}
135
+
```
136
+
91
137
### Internal change to use multi-phase initialization
92
138
93
139
[PEP 489](https://peps.python.org/pep-0489/) introduced "multi-phase initialization" for extension modules which provides ways to allocate and clean up per-module state.
0 commit comments