Skip to content

Commit d5452bc

Browse files
authored
feature gate deprecated APIs for PyType, PyTypeInfo and PySuper (#4134)
1 parent 9e1960e commit d5452bc

File tree

7 files changed

+33
-44
lines changed

7 files changed

+33
-44
lines changed

guide/src/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ To migrate, switch all type casts to use `obj.downcast()` instead of `try_from(o
6060

6161
Before:
6262

63-
```rust
63+
```rust,ignore
6464
# #![allow(deprecated)]
6565
# use pyo3::prelude::*;
6666
# use pyo3::types::{PyInt, PyList};

src/conversion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::err::{self, PyDowncastError, PyResult};
33
#[cfg(feature = "experimental-inspect")]
44
use crate::inspect::types::TypeInfo;
55
use crate::pyclass::boolean_struct::False;
6-
use crate::type_object::PyTypeInfo;
76
use crate::types::any::PyAnyMethods;
87
use crate::types::PyTuple;
98
use crate::{
@@ -435,9 +434,11 @@ pub trait PyTryInto<T>: Sized {
435434
fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>;
436435
}
437436

437+
#[cfg(feature = "gil-refs")]
438438
#[allow(deprecated)]
439439
mod implementations {
440440
use super::*;
441+
use crate::type_object::PyTypeInfo;
441442

442443
// TryFrom implies TryInto
443444
impl<U> PyTryInto<U> for PyAny

src/instance.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,8 +2310,6 @@ a = A()
23102310

23112311
#[cfg(feature = "macros")]
23122312
mod using_macros {
2313-
use crate::PyCell;
2314-
23152313
use super::*;
23162314

23172315
#[crate::pyclass(crate = "crate")]
@@ -2371,9 +2369,10 @@ a = A()
23712369
}
23722370

23732371
#[test]
2372+
#[cfg(feature = "gil-refs")]
23742373
#[allow(deprecated)]
23752374
fn cell_tryfrom() {
2376-
use crate::PyTryInto;
2375+
use crate::{PyCell, PyTryInto};
23772376
// More detailed tests of the underlying semantics in pycell.rs
23782377
Python::with_gil(|py| {
23792378
let instance: &PyAny = Py::new(py, SomeClass(0)).unwrap().into_ref(py);

src/type_object.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ pub unsafe trait PyTypeInfo: Sized + HasPyGilRef {
6666

6767
/// Returns the safe abstraction over the type object.
6868
#[inline]
69-
#[cfg_attr(
70-
not(feature = "gil-refs"),
71-
deprecated(
72-
since = "0.21.0",
73-
note = "`PyTypeInfo::type_object` will be replaced by `PyTypeInfo::type_object_bound` in a future PyO3 version"
74-
)
69+
#[cfg(feature = "gil-refs")]
70+
#[deprecated(
71+
since = "0.21.0",
72+
note = "`PyTypeInfo::type_object` will be replaced by `PyTypeInfo::type_object_bound` in a future PyO3 version"
7573
)]
7674
fn type_object(py: Python<'_>) -> &PyType {
7775
// 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 {
10199

102100
/// Checks if `object` is an instance of this type or a subclass of this type.
103101
#[inline]
104-
#[cfg_attr(
105-
not(feature = "gil-refs"),
106-
deprecated(
107-
since = "0.21.0",
108-
note = "`PyTypeInfo::is_type_of` will be replaced by `PyTypeInfo::is_type_of_bound` in a future PyO3 version"
109-
)
102+
#[cfg(feature = "gil-refs")]
103+
#[deprecated(
104+
since = "0.21.0",
105+
note = "`PyTypeInfo::is_type_of` will be replaced by `PyTypeInfo::is_type_of_bound` in a future PyO3 version"
110106
)]
111107
fn is_type_of(object: &PyAny) -> bool {
112108
Self::is_type_of_bound(&object.as_borrowed())
@@ -120,12 +116,10 @@ pub unsafe trait PyTypeInfo: Sized + HasPyGilRef {
120116

121117
/// Checks if `object` is an instance of this type.
122118
#[inline]
123-
#[cfg_attr(
124-
not(feature = "gil-refs"),
125-
deprecated(
126-
since = "0.21.0",
127-
note = "`PyTypeInfo::is_exact_type_of` will be replaced by `PyTypeInfo::is_exact_type_of_bound` in a future PyO3 version"
128-
)
119+
#[cfg(feature = "gil-refs")]
120+
#[deprecated(
121+
since = "0.21.0",
122+
note = "`PyTypeInfo::is_exact_type_of` will be replaced by `PyTypeInfo::is_exact_type_of_bound` in a future PyO3 version"
129123
)]
130124
fn is_exact_type_of(object: &PyAny) -> bool {
131125
Self::is_exact_type_of_bound(&object.as_borrowed())

src/types/any.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,9 +2726,9 @@ class SimpleClass:
27262726
#[test]
27272727
fn test_is_callable() {
27282728
Python::with_gil(|py| {
2729-
assert!(PyList::type_object(py).is_callable());
2729+
assert!(PyList::type_object_bound(py).is_callable());
27302730

2731-
let not_callable = 5.to_object(py).into_ref(py);
2731+
let not_callable = 5.to_object(py).into_bound(py);
27322732
assert!(!not_callable.is_callable());
27332733
});
27342734
}

src/types/pysuper.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::instance::Bound;
22
use crate::types::any::PyAnyMethods;
33
use crate::types::PyType;
4-
use crate::{ffi, PyNativeType, PyTypeInfo};
4+
use crate::{ffi, PyTypeInfo};
55
use crate::{PyAny, PyResult};
66

77
/// Represents a Python `super` object.
@@ -17,14 +17,13 @@ pyobject_native_type_core!(
1717

1818
impl PySuper {
1919
/// Deprecated form of `PySuper::new_bound`.
20-
#[cfg_attr(
21-
not(feature = "gil-refs"),
22-
deprecated(
23-
since = "0.21.0",
24-
note = "`PySuper::new` will be replaced by `PySuper::new_bound` in a future PyO3 version"
25-
)
20+
#[cfg(feature = "gil-refs")]
21+
#[deprecated(
22+
since = "0.21.0",
23+
note = "`PySuper::new` will be replaced by `PySuper::new_bound` in a future PyO3 version"
2624
)]
2725
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> {
26+
use crate::PyNativeType;
2827
Self::new_bound(&ty.as_borrowed(), &obj.as_borrowed()).map(Bound::into_gil_ref)
2928
}
3029

src/types/typeobject.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ pyobject_native_type_core!(PyType, pyobject_native_static_type_object!(ffi::PyTy
1515
impl PyType {
1616
/// Deprecated form of [`PyType::new_bound`].
1717
#[inline]
18-
#[cfg_attr(
19-
not(feature = "gil-refs"),
20-
deprecated(
21-
since = "0.21.0",
22-
note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version"
23-
)
18+
#[cfg(feature = "gil-refs")]
19+
#[deprecated(
20+
since = "0.21.0",
21+
note = "`PyType::new` will be replaced by `PyType::new_bound` in a future PyO3 version"
2422
)]
2523
pub fn new<T: PyTypeInfo>(py: Python<'_>) -> &PyType {
2624
T::type_object_bound(py).into_gil_ref()
@@ -44,12 +42,10 @@ impl PyType {
4442
///
4543
/// - The pointer must a valid non-null reference to a `PyTypeObject`.
4644
#[inline]
47-
#[cfg_attr(
48-
not(feature = "gil-refs"),
49-
deprecated(
50-
since = "0.21.0",
51-
note = "Use `PyType::from_borrowed_type_ptr` instead"
52-
)
45+
#[cfg(feature = "gil-refs")]
46+
#[deprecated(
47+
since = "0.21.0",
48+
note = "Use `PyType::from_borrowed_type_ptr` instead"
5349
)]
5450
pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType {
5551
Self::from_borrowed_type_ptr(py, p).into_gil_ref()

0 commit comments

Comments
 (0)