Skip to content

Commit 1c764cd

Browse files
committed
docs: improve messaging around #[pyclass(from_py_object)] change (#5798)
* docs: improve messaging around `#[pyclass(from_py_object)]` change * fmt * add justification to deprecation * fix migration guide entry
1 parent 927411a commit 1c764cd

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

guide/src/migration.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,41 @@ Modules now automatically allow use on free-threaded Python, unless they directl
2222
<summary><small>Click to expand</small></summary>
2323

2424
`#[pyclass]` types which implement `Clone` used to also implement `FromPyObject` automatically.
25-
This behaviour is phased out and replaced by an explicit opt-in.
25+
This behavior is being phased out and replaced by an explicit opt-in, which will allow [better error messages and more user control](https://github.com/PyO3/pyo3/issues/5419).
2626
Affected types will by marked by a deprecation message.
27+
2728
To migrate use either
2829

29-
- `from_py_object` to keep the automatic derive, or
30-
- `skip_from_py_object` to accept the new behaviour
30+
- `#[pyclass(from_py_object)]` to keep the automatic derive, or
31+
- `#[pyclass(skip_from_py_object)]` to accept the new behavior.
32+
33+
Before:
34+
35+
```rust
36+
# #![allow(deprecated)]
37+
# use pyo3::prelude::*;
38+
#[pyclass]
39+
#[derive(Clone)]
40+
struct PyClass {}
41+
```
42+
43+
After:
44+
45+
```rust
46+
# use pyo3::prelude::*;
47+
// If the automatic implementation of `FromPyObject` is desired, opt in:
48+
#[pyclass(from_py_object)]
49+
#[derive(Clone)]
50+
struct PyClass {}
51+
52+
// or if the `FromPyObject` implementation is not needed:
53+
#[pyclass(skip_from_py_object)]
54+
#[derive(Clone)]
55+
struct PyClassWithoutFromPyObject {}
56+
```
57+
58+
The `#[pyclass(skip_from_py_object)]` option will eventually be deprecated and removed as it becomes the default behavior.
59+
3160
</details>
3261

3362
### Deprecation of `Py<T>` constructors from raw pointer
@@ -44,7 +73,7 @@ These should be used instead.
4473
Before:
4574

4675
```rust
47-
#![allow(deprecated)]
76+
# #![allow(deprecated)]
4877
# use pyo3::prelude::*;
4978
# use pyo3::types::PyNone;
5079
# Python::attach(|py| {

src/impl_/deprecated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub struct HasAutomaticFromPyObject<const IS_CLONE: bool> {}
33
impl HasAutomaticFromPyObject<true> {
44
#[deprecated(
55
since = "0.28.0",
6-
note = "The automatically derived `FromPyObject` implementation for `#[pyclass]` types which implement `Clone` is being phased out. Use `from_py_object` to keep the automatic derive or `skip_from_py_object` to accept the new behaviour."
6+
note = "The `FromPyObject` implementation for `#[pyclass]` types which implement `Clone` is changing to an opt-in option. Use `#[pyclass(from_py_object)]` to opt-in to the `FromPyObject` derive now, or `#[pyclass(skip_from_py_object)]` to skip the `FromPyObject` implementation."
77
)]
88
pub const MSG: () = ();
99
}

tests/ui/invalid_pyclass_args.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ error[E0609]: no field `162543` on type `&Coord3`
443443
|
444444
= note: available fields are: `0`, `1`, `2`
445445

446-
warning: use of deprecated associated constant `pyo3::impl_::deprecated::HasAutomaticFromPyObject::<true>::MSG`: The automatically derived `FromPyObject` implementation for `#[pyclass]` types which implement `Clone` is being phased out. Use `from_py_object` to keep the automatic derive or `skip_from_py_object` to accept the new behaviour.
446+
warning: use of deprecated associated constant `pyo3::impl_::deprecated::HasAutomaticFromPyObject::<true>::MSG`: The `FromPyObject` implementation for `#[pyclass]` types which implement `Clone` is changing to an opt-in option. Use `#[pyclass(from_py_object)]` to opt-in to the `FromPyObject` derive now, or `#[pyclass(skip_from_py_object)]` to skip the `FromPyObject` implementation.
447447
--> tests/ui/invalid_pyclass_args.rs:196:1
448448
|
449449
196 | #[pyclass]

0 commit comments

Comments
 (0)