feature gate deprecated APIs for `PyType`, `PyTypeInfo` and `PySuper` (#4134)

This commit is contained in:
Icxolu 2024-04-28 23:03:51 +02:00 committed by GitHub
parent 9e1960ea34
commit d5452bcd8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 33 additions and 44 deletions

View File

@ -60,7 +60,7 @@ To migrate, switch all type casts to use `obj.downcast()` instead of `try_from(o
Before: Before:
```rust ```rust,ignore
# #![allow(deprecated)] # #![allow(deprecated)]
# use pyo3::prelude::*; # use pyo3::prelude::*;
# use pyo3::types::{PyInt, PyList}; # use pyo3::types::{PyInt, PyList};

View File

@ -3,7 +3,6 @@ use crate::err::{self, PyDowncastError, PyResult};
#[cfg(feature = "experimental-inspect")] #[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo; use crate::inspect::types::TypeInfo;
use crate::pyclass::boolean_struct::False; use crate::pyclass::boolean_struct::False;
use crate::type_object::PyTypeInfo;
use crate::types::any::PyAnyMethods; use crate::types::any::PyAnyMethods;
use crate::types::PyTuple; use crate::types::PyTuple;
use crate::{ use crate::{
@ -435,9 +434,11 @@ pub trait PyTryInto<T>: Sized {
fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>; fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>;
} }
#[cfg(feature = "gil-refs")]
#[allow(deprecated)] #[allow(deprecated)]
mod implementations { mod implementations {
use super::*; use super::*;
use crate::type_object::PyTypeInfo;
// TryFrom implies TryInto // TryFrom implies TryInto
impl<U> PyTryInto<U> for PyAny impl<U> PyTryInto<U> for PyAny

View File

@ -2310,8 +2310,6 @@ a = A()
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
mod using_macros { mod using_macros {
use crate::PyCell;
use super::*; use super::*;
#[crate::pyclass(crate = "crate")] #[crate::pyclass(crate = "crate")]
@ -2371,9 +2369,10 @@ a = A()
} }
#[test] #[test]
#[cfg(feature = "gil-refs")]
#[allow(deprecated)] #[allow(deprecated)]
fn cell_tryfrom() { fn cell_tryfrom() {
use crate::PyTryInto; use crate::{PyCell, PyTryInto};
// More detailed tests of the underlying semantics in pycell.rs // More detailed tests of the underlying semantics in pycell.rs
Python::with_gil(|py| { Python::with_gil(|py| {
let instance: &PyAny = Py::new(py, SomeClass(0)).unwrap().into_ref(py); let instance: &PyAny = Py::new(py, SomeClass(0)).unwrap().into_ref(py);

View File

@ -66,12 +66,10 @@ pub unsafe trait PyTypeInfo: Sized + HasPyGilRef {
/// Returns the safe abstraction over the type object. /// Returns the safe abstraction over the type object.
#[inline] #[inline]
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "`PyTypeInfo::type_object` will be replaced by `PyTypeInfo::type_object_bound` in a future PyO3 version"
note = "`PyTypeInfo::type_object` will be replaced by `PyTypeInfo::type_object_bound` in a future PyO3 version"
)
)] )]
fn type_object(py: Python<'_>) -> &PyType { fn type_object(py: Python<'_>) -> &PyType {
// This isn't implemented in terms of `type_object_bound` because this just borrowed the // This isn't implemented in terms of `type_object_bound` because this just borrowed the
@ -101,12 +99,10 @@ pub unsafe trait PyTypeInfo: Sized + HasPyGilRef {
/// Checks if `object` is an instance of this type or a subclass of this type. /// Checks if `object` is an instance of this type or a subclass of this type.
#[inline] #[inline]
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "`PyTypeInfo::is_type_of` will be replaced by `PyTypeInfo::is_type_of_bound` in a future PyO3 version"
note = "`PyTypeInfo::is_type_of` will be replaced by `PyTypeInfo::is_type_of_bound` in a future PyO3 version"
)
)] )]
fn is_type_of(object: &PyAny) -> bool { fn is_type_of(object: &PyAny) -> bool {
Self::is_type_of_bound(&object.as_borrowed()) Self::is_type_of_bound(&object.as_borrowed())
@ -120,12 +116,10 @@ pub unsafe trait PyTypeInfo: Sized + HasPyGilRef {
/// Checks if `object` is an instance of this type. /// Checks if `object` is an instance of this type.
#[inline] #[inline]
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "`PyTypeInfo::is_exact_type_of` will be replaced by `PyTypeInfo::is_exact_type_of_bound` in a future PyO3 version"
note = "`PyTypeInfo::is_exact_type_of` will be replaced by `PyTypeInfo::is_exact_type_of_bound` in a future PyO3 version"
)
)] )]
fn is_exact_type_of(object: &PyAny) -> bool { fn is_exact_type_of(object: &PyAny) -> bool {
Self::is_exact_type_of_bound(&object.as_borrowed()) Self::is_exact_type_of_bound(&object.as_borrowed())

View File

@ -2726,9 +2726,9 @@ class SimpleClass:
#[test] #[test]
fn test_is_callable() { fn test_is_callable() {
Python::with_gil(|py| { Python::with_gil(|py| {
assert!(PyList::type_object(py).is_callable()); assert!(PyList::type_object_bound(py).is_callable());
let not_callable = 5.to_object(py).into_ref(py); let not_callable = 5.to_object(py).into_bound(py);
assert!(!not_callable.is_callable()); assert!(!not_callable.is_callable());
}); });
} }

View File

@ -1,7 +1,7 @@
use crate::instance::Bound; use crate::instance::Bound;
use crate::types::any::PyAnyMethods; use crate::types::any::PyAnyMethods;
use crate::types::PyType; use crate::types::PyType;
use crate::{ffi, PyNativeType, PyTypeInfo}; use crate::{ffi, PyTypeInfo};
use crate::{PyAny, PyResult}; use crate::{PyAny, PyResult};
/// Represents a Python `super` object. /// Represents a Python `super` object.
@ -17,14 +17,13 @@ pyobject_native_type_core!(
impl PySuper { impl PySuper {
/// Deprecated form of `PySuper::new_bound`. /// Deprecated form of `PySuper::new_bound`.
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "`PySuper::new` will be replaced by `PySuper::new_bound` in a future PyO3 version"
note = "`PySuper::new` will be replaced by `PySuper::new_bound` in a future PyO3 version"
)
)] )]
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> { pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> {
use crate::PyNativeType;
Self::new_bound(&ty.as_borrowed(), &obj.as_borrowed()).map(Bound::into_gil_ref) Self::new_bound(&ty.as_borrowed(), &obj.as_borrowed()).map(Bound::into_gil_ref)
} }

View File

@ -15,12 +15,10 @@ pyobject_native_type_core!(PyType, pyobject_native_static_type_object!(ffi::PyTy
impl PyType { impl PyType {
/// Deprecated form of [`PyType::new_bound`]. /// Deprecated form of [`PyType::new_bound`].
#[inline] #[inline]
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version"
note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version"
)
)] )]
pub fn new<T: PyTypeInfo>(py: Python<'_>) -> &PyType { pub fn new<T: PyTypeInfo>(py: Python<'_>) -> &PyType {
T::type_object_bound(py).into_gil_ref() T::type_object_bound(py).into_gil_ref()
@ -44,12 +42,10 @@ impl PyType {
/// ///
/// - The pointer must a valid non-null reference to a `PyTypeObject`. /// - The pointer must a valid non-null reference to a `PyTypeObject`.
#[inline] #[inline]
#[cfg_attr( #[cfg(feature = "gil-refs")]
not(feature = "gil-refs"), #[deprecated(
deprecated( since = "0.21.0",
since = "0.21.0", note = "Use `PyType::from_borrowed_type_ptr` instead"
note = "Use `PyType::from_borrowed_type_ptr` instead"
)
)] )]
pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType { pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType {
Self::from_borrowed_type_ptr(py, p).into_gil_ref() Self::from_borrowed_type_ptr(py, p).into_gil_ref()