Skip to content

Commit 18aa0c2

Browse files
authored
docs: fix some sections in the migration guide (#5763)
* docs: fix some sections in the migration guide * fix fmt
1 parent 8848c76 commit 18aa0c2

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

guide/src/migration.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@ For a detailed list of all changes, see the [CHANGELOG](changelog.md).
77

88
### Default to supporting free-threaded Python
99

10+
<details open>
11+
<summary><small>Click to expand</small></summary>
12+
1013
When PyO3 0.23 added support for free-threaded Python, this was as an opt-in feature for modules by annotating with `#[pymodule(gil_used = false)]`.
1114

1215
As the support has matured and PyO3's own API has evolved to remove reliance on the GIL, the time is right to switch the default.
1316
Modules now automatically allow use on free-threaded Python, unless they directly state they require the GIL with `#[pymodule(gil_used = true)]`.
17+
</details>
1418

1519
### Deprecation of automatic `FromPyObject` for `#[pyclass]` types which implement `Clone`
1620

21+
<details open>
22+
<summary><small>Click to expand</small></summary>
23+
1724
`#[pyclass]` types which implement `Clone` used to also implement `FromPyObject` automatically.
1825
This behaviour is phased out and replaced by an explicit opt-in.
1926
Affected types will by marked by a deprecation message.
2027
To migrate use either
2128

2229
- `from_py_object` to keep the automatic derive, or
2330
- `skip_from_py_object` to accept the new behaviour
31+
</details>
2432

2533
### Deprecation of `Py<T>` constructors from raw pointer
2634

35+
<details open>
36+
<summary><small>Click to expand</small></summary>
37+
2738
The constructors `Py::from_owned_ptr`, `Py::from_owned_ptr_or_opt`, and `Py::from_owned_ptr_or_err` (and similar "borrowed" variants) perform an unchecked cast to the `Py<T>` target type `T`.
2839
This unchecked cast is a footgun on APIs where the primary concern is about constructing PyO3's safe smart pointer types correctly from the raw pointer value.
2940

@@ -59,8 +70,13 @@ let _: Bound<'_, PyNone> = unsafe { Bound::from_owned_ptr(py, raw_ptr).cast_into
5970
# })
6071
```
6172

73+
</details>
74+
6275
### Removal of `From<Bound<'_, T>` and `From<Py<T>> for PyClassInitializer<T>`
6376

77+
<details open>
78+
<summary><small>Click to expand</small></summary>
79+
6480
As part of refactoring the initialization code these impls were removed and its functionality was moved into the generated code for `#[new]`.
6581
As a small side side effect the following pattern will not be accepted anymore:
6682

@@ -88,8 +104,13 @@ let obj_2 = existing_bound.clone();
88104
# })
89105
```
90106

107+
</details>
108+
91109
### Internal change to use multi-phase initialization
92110

111+
<details open>
112+
<summary><small>Click to expand</small></summary>
113+
93114
[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.
94115
This is a necessary step towards supporting Python "subinterpreters" which run on their own copy of state.
95116

@@ -98,12 +119,13 @@ The possibility of creating and consuming per-module state (and supporting subin
98119
This should not require migration, nor is there expected to be breakage caused by the change.
99120

100121
Nevertheless, this affects the order of initialization so seemed worth noting in this guide.
122+
</details>
101123

102124
## from 0.26.* to 0.27
103125

104126
### `FromPyObject` reworked for flexibility and efficiency
105127

106-
<details open>
128+
<details>
107129
<summary><small>Click to expand</small></summary>
108130

109131
With the removal of the `gil-ref` API in PyO3 0.23 it is now possible to fully split the Python lifetime `'py` and the input lifetime `'a`.
@@ -236,7 +258,10 @@ This is very similar to `serde`s [`Deserialize`] and [`DeserializeOwned`] traits
236258
[`DeserializeOwned`]: https://docs.rs/serde/latest/serde/de/trait.DeserializeOwned.html
237259
</details>
238260

239-
## `.downcast()` and `DowncastError` replaced with `.cast()` and `CastError`
261+
### `.downcast()` and `DowncastError` replaced with `.cast()` and `CastError`
262+
263+
<details>
264+
<summary><small>Click to expand</small></summary>
240265

241266
The `.downcast()` family of functions were only available on `Bound<PyAny>`.
242267
In corner cases (particularly related to `.downcast_into()`) this would require use of `.as_any().downcast()` or `.into_any().downcast_into()` chains.
@@ -248,18 +273,23 @@ This produces a nicer experience for both PyO3 module authors and consumers.
248273
To migrate, replace `.downcast()` with `.cast()` and `DowncastError` with `CastError` (and similar with `.downcast_into()` / `DowncastIntoError` etc).
249274

250275
`CastError` requires a Python `type` object (or other "classinfo" object compatible with `isinstance()`) as the second object, so in the rare case where `DowncastError` was manually constructed, small adjustments to code may apply.
276+
</details>
251277

252-
## `PyTypeCheck` is now an `unsafe trait`
278+
### `PyTypeCheck` is now an `unsafe trait`
279+
280+
<details>
281+
<summary><small>Click to expand</small></summary>
253282

254283
Because `PyTypeCheck` is the trait used to guard the `.cast()` functions to treat Python objects as specific concrete types, the trait is `unsafe` to implement.
255284

256285
This should always have been the case, it was an unfortunate omission from its original implementation which is being corrected in this release.
286+
</details>
257287

258288
## from 0.25.* to 0.26
259289

260290
### Rename of `Python::with_gil`, `Python::allow_threads`, and `pyo3::prepare_freethreaded_python`
261291

262-
<details open>
292+
<details>
263293
<summary><small>Click to expand</small></summary>
264294

265295
The names for these APIs were created when the global interpreter lock (GIL) was mandatory.
@@ -274,7 +304,7 @@ For this reason, we chose to rename these to more modern terminology introduced
274304

275305
### Deprecation of `PyObject` type alias
276306

277-
<details open>
307+
<details>
278308
<summary><small>Click to expand</small></summary>
279309

280310
The type alias `PyObject` (aka `Py<PyAny>`) is often confused with the identically named FFI definition `pyo3::ffi::PyObject`.
@@ -284,7 +314,7 @@ To migrate simply replace its usage by the target type `Py<PyAny>`.
284314

285315
### Replacement of `GILOnceCell` with `PyOnceLock`
286316

287-
<details open>
317+
<details>
288318
<summary><small>Click to expand</small></summary>
289319

290320
Similar to the above renaming of `Python::with_gil` and related APIs, the `GILOnceCell` type was designed for a Python interpreter which was limited by the GIL.
@@ -327,7 +357,7 @@ DECIMAL_TYPE.import(py, "decimal", "Decimal")?;
327357

328358
### Deprecation of `GILProtected`
329359

330-
<details open>
360+
<details>
331361
<summary><small>Click to expand</small></summary>
332362

333363
As another cleanup related to concurrency primitives designed for a Python constrained by the GIL, the `GILProtected` type is now deprecated.

0 commit comments

Comments
 (0)