Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate ToPyObject in favor or IntoPyObject #4595

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,10 @@ impl SubSubClass {
let base = PyClassInitializer::from(BaseClass::new());
let sub = base.add_subclass(SubClass { val2: val });
if val % 2 == 0 {
Ok(Py::new(py, sub)?.to_object(py))
Ok(Py::new(py, sub)?.into_any())
} else {
let sub_sub = sub.add_subclass(SubSubClass { val3: val });
Ok(Py::new(py, sub_sub)?.to_object(py))
Ok(Py::new(py, sub_sub)?.into_any())
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ enum RustyEnum<'py> {
# fn main() -> PyResult<()> {
# Python::with_gil(|py| -> PyResult<()> {
# {
# let thing = 42_u8.to_object(py);
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
# let thing = 42_u8.into_pyobject(py)?;
# let rust_thing: RustyEnum<'_> = thing.extract()?;
#
# assert_eq!(
# 42,
Expand All @@ -318,8 +318,8 @@ enum RustyEnum<'py> {
# );
# }
# {
# let thing = (32_u8, 73_u8).to_object(py);
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
# let thing = (32_u8, 73_u8).into_pyobject(py)?;
# let rust_thing: RustyEnum<'_> = thing.extract()?;
#
# assert_eq!(
# (32, 73),
Expand All @@ -330,8 +330,8 @@ enum RustyEnum<'py> {
# );
# }
# {
# let thing = ("foo", 73_u8).to_object(py);
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
# let thing = ("foo", 73_u8).into_pyobject(py)?;
# let rust_thing: RustyEnum<'_> = thing.extract()?;
#
# assert_eq!(
# (String::from("foo"), 73),
Expand Down Expand Up @@ -427,8 +427,8 @@ enum RustyEnum {
# fn main() -> PyResult<()> {
# Python::with_gil(|py| -> PyResult<()> {
# {
# let thing = 42_u8.to_object(py);
# let rust_thing: RustyEnum = thing.extract(py)?;
# let thing = 42_u8.into_pyobject(py)?;
# let rust_thing: RustyEnum = thing.extract()?;
#
# assert_eq!(
# 42,
Expand All @@ -440,8 +440,8 @@ enum RustyEnum {
# }
#
# {
# let thing = "foo".to_object(py);
# let rust_thing: RustyEnum = thing.extract(py)?;
# let thing = "foo".into_pyobject(py)?;
# let rust_thing: RustyEnum = thing.extract()?;
#
# assert_eq!(
# "foo",
Expand All @@ -453,8 +453,8 @@ enum RustyEnum {
# }
#
# {
# let thing = b"foo".to_object(py);
# let error = thing.extract::<RustyEnum>(py).unwrap_err();
# let thing = b"foo".into_pyobject(py)?;
# let error = thing.extract::<RustyEnum>().unwrap_err();
# assert!(error.is_instance_of::<pyo3::exceptions::PyTypeError>(py));
# }
#
Expand Down
5 changes: 4 additions & 1 deletion guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@ Notable features of this new trait:
All PyO3 provided types as well as `#[pyclass]`es already implement `IntoPyObject`. Other types will
need to adapt an implementation of `IntoPyObject` to stay compatible with the Python APIs.

Together with the introduction of `IntoPyObject` the old conversion traits `ToPyObject` and `IntoPy`
are deprecated and will be removed in a future PyO3 version.


Before:
```rust
```rust,ignore
# use pyo3::prelude::*;
# #[allow(dead_code)]
struct MyPyObjectWrapper(PyObject);
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/4595.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `PyErr::matches` is now fallible due to `IntoPyObject` migration.
- deprecate `ToPyObject` in favor of `IntoPyObject`
4 changes: 2 additions & 2 deletions pytests/src/objstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl ObjStore {
ObjStore::default()
}

fn push(&mut self, py: Python<'_>, obj: &Bound<'_, PyAny>) {
self.obj.push(obj.to_object(py));
fn push(&mut self, obj: &Bound<'_, PyAny>) {
self.obj.push(obj.clone().unbind());
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub unsafe trait AsPyPointer {
}

/// Conversion trait that allows various objects to be converted into `PyObject`.
#[deprecated(
since = "0.23.0",
note = "`ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23/migration) for more information."
)]
pub trait ToPyObject {
/// Converts self into a Python object.
fn to_object(&self, py: Python<'_>) -> PyObject;
Expand Down Expand Up @@ -548,6 +552,7 @@ where

/// Identity conversion: allows using existing `PyObject` instances where
/// `T: ToPyObject` is expected.
#[allow(deprecated)]
impl<T: ?Sized + ToPyObject> ToPyObject for &'_ T {
#[inline]
fn to_object(&self, py: Python<'_>) -> PyObject {
Expand Down
Loading
Loading