Skip to content

Commit

Permalink
feature gate deprecated APIs for PyFrozenSet (PyO3#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Apr 24, 2024
1 parent c951bf8 commit 6c2e6f8
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/types/frozenset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ impl<'py> PyFrozenSetBuilder<'py> {
}

/// Deprecated form of [`PyFrozenSetBuilder::finalize_bound`]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyFrozenSetBuilder::finalize` will be replaced by `PyFrozenSetBuilder::finalize_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyFrozenSetBuilder::finalize` will be replaced by `PyFrozenSetBuilder::finalize_bound` in a future PyO3 version"
)]
pub fn finalize(self) -> &'py PyFrozenSet {
self.finalize_bound().into_gil_ref()
Expand Down Expand Up @@ -78,12 +76,10 @@ pyobject_native_type_core!(
impl PyFrozenSet {
/// Deprecated form of [`PyFrozenSet::new_bound`].
#[inline]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyFrozenSet::new` will be replaced by `PyFrozenSet::new_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyFrozenSet::new` will be replaced by `PyFrozenSet::new_bound` in a future PyO3 version"
)]
pub fn new<'a, 'p, T: ToPyObject + 'a>(
py: Python<'p>,
Expand All @@ -104,12 +100,10 @@ impl PyFrozenSet {
}

/// Deprecated form of [`PyFrozenSet::empty_bound`].
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyFrozenSet::empty` will be replaced by `PyFrozenSet::empty_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyFrozenSet::empty` will be replaced by `PyFrozenSet::empty_bound` in a future PyO3 version"
)]
pub fn empty(py: Python<'_>) -> PyResult<&'_ PyFrozenSet> {
Self::empty_bound(py).map(Bound::into_gil_ref)
Expand Down Expand Up @@ -324,25 +318,24 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
}

#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
use super::*;

#[test]
fn test_frozenset_new_and_len() {
Python::with_gil(|py| {
let set = PyFrozenSet::new(py, &[1]).unwrap();
let set = PyFrozenSet::new_bound(py, &[1]).unwrap();
assert_eq!(1, set.len());

let v = vec![1];
assert!(PyFrozenSet::new(py, &[v]).is_err());
assert!(PyFrozenSet::new_bound(py, &[v]).is_err());
});
}

#[test]
fn test_frozenset_empty() {
Python::with_gil(|py| {
let set = PyFrozenSet::empty(py).unwrap();
let set = PyFrozenSet::empty_bound(py).unwrap();
assert_eq!(0, set.len());
assert!(set.is_empty());
});
Expand All @@ -351,15 +344,15 @@ mod tests {
#[test]
fn test_frozenset_contains() {
Python::with_gil(|py| {
let set = PyFrozenSet::new(py, &[1]).unwrap();
let set = PyFrozenSet::new_bound(py, &[1]).unwrap();
assert!(set.contains(1).unwrap());
});
}

#[test]
fn test_frozenset_iter() {
Python::with_gil(|py| {
let set = PyFrozenSet::new(py, &[1]).unwrap();
let set = PyFrozenSet::new_bound(py, &[1]).unwrap();

for el in set {
assert_eq!(1i32, el.extract::<i32>().unwrap());
Expand All @@ -381,7 +374,7 @@ mod tests {
#[test]
fn test_frozenset_iter_size_hint() {
Python::with_gil(|py| {
let set = PyFrozenSet::new(py, &[1]).unwrap();
let set = PyFrozenSet::new_bound(py, &[1]).unwrap();
let mut iter = set.iter();

// Exact size
Expand Down

0 comments on commit 6c2e6f8

Please sign in to comment.