Skip to content

Commit

Permalink
deprecate ToPyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Oct 4, 2024
1 parent e9a1b9b commit 0374af6
Show file tree
Hide file tree
Showing 58 changed files with 681 additions and 487 deletions.
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
2 changes: 1 addition & 1 deletion guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ need to adapt an implementation of `IntoPyObject` to stay compatible with the Py


Before:
```rust
```rust,ignore
# use pyo3::prelude::*;
# #[allow(dead_code)]
struct MyPyObjectWrapper(PyObject);
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/xxxx.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`
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 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

0 comments on commit 0374af6

Please sign in to comment.