Skip to content

Commit d42c00d

Browse files
authored
feature gate deprecated APIs for PySet (#4096)
1 parent b11174e commit d42c00d

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/types/set.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ pyobject_native_type_core!(
3131

3232
impl PySet {
3333
/// Deprecated form of [`PySet::new_bound`].
34-
#[cfg_attr(
35-
not(feature = "gil-refs"),
36-
deprecated(
37-
since = "0.21.0",
38-
note = "`PySet::new` will be replaced by `PySet::new_bound` in a future PyO3 version"
39-
)
34+
#[cfg(feature = "gil-refs")]
35+
#[deprecated(
36+
since = "0.21.0",
37+
note = "`PySet::new` will be replaced by `PySet::new_bound` in a future PyO3 version"
4038
)]
4139
#[inline]
4240
pub fn new<'a, 'p, T: ToPyObject + 'a>(
@@ -58,12 +56,10 @@ impl PySet {
5856
}
5957

6058
/// Deprecated form of [`PySet::empty_bound`].
61-
#[cfg_attr(
62-
not(feature = "gil-refs"),
63-
deprecated(
64-
since = "0.21.2",
65-
note = "`PySet::empty` will be replaced by `PySet::empty_bound` in a future PyO3 version"
66-
)
59+
#[cfg(feature = "gil-refs")]
60+
#[deprecated(
61+
since = "0.21.2",
62+
note = "`PySet::empty` will be replaced by `PySet::empty_bound` in a future PyO3 version"
6763
)]
6864
pub fn empty(py: Python<'_>) -> PyResult<&PySet> {
6965
Self::empty_bound(py).map(Bound::into_gil_ref)
@@ -396,27 +392,29 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
396392
}
397393

398394
#[cfg(test)]
399-
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
400395
mod tests {
401396
use super::PySet;
402-
use crate::{Python, ToPyObject};
397+
use crate::{
398+
types::{PyAnyMethods, PySetMethods},
399+
Python, ToPyObject,
400+
};
403401
use std::collections::HashSet;
404402

405403
#[test]
406404
fn test_set_new() {
407405
Python::with_gil(|py| {
408-
let set = PySet::new(py, &[1]).unwrap();
406+
let set = PySet::new_bound(py, &[1]).unwrap();
409407
assert_eq!(1, set.len());
410408

411409
let v = vec![1];
412-
assert!(PySet::new(py, &[v]).is_err());
410+
assert!(PySet::new_bound(py, &[v]).is_err());
413411
});
414412
}
415413

416414
#[test]
417415
fn test_set_empty() {
418416
Python::with_gil(|py| {
419-
let set = PySet::empty(py).unwrap();
417+
let set = PySet::empty_bound(py).unwrap();
420418
assert_eq!(0, set.len());
421419
assert!(set.is_empty());
422420
});
@@ -427,19 +425,19 @@ mod tests {
427425
Python::with_gil(|py| {
428426
let mut v = HashSet::new();
429427
let ob = v.to_object(py);
430-
let set: &PySet = ob.downcast(py).unwrap();
428+
let set = ob.downcast_bound::<PySet>(py).unwrap();
431429
assert_eq!(0, set.len());
432430
v.insert(7);
433431
let ob = v.to_object(py);
434-
let set2: &PySet = ob.downcast(py).unwrap();
432+
let set2 = ob.downcast_bound::<PySet>(py).unwrap();
435433
assert_eq!(1, set2.len());
436434
});
437435
}
438436

439437
#[test]
440438
fn test_set_clear() {
441439
Python::with_gil(|py| {
442-
let set = PySet::new(py, &[1]).unwrap();
440+
let set = PySet::new_bound(py, &[1]).unwrap();
443441
assert_eq!(1, set.len());
444442
set.clear();
445443
assert_eq!(0, set.len());
@@ -449,15 +447,15 @@ mod tests {
449447
#[test]
450448
fn test_set_contains() {
451449
Python::with_gil(|py| {
452-
let set = PySet::new(py, &[1]).unwrap();
450+
let set = PySet::new_bound(py, &[1]).unwrap();
453451
assert!(set.contains(1).unwrap());
454452
});
455453
}
456454

457455
#[test]
458456
fn test_set_discard() {
459457
Python::with_gil(|py| {
460-
let set = PySet::new(py, &[1]).unwrap();
458+
let set = PySet::new_bound(py, &[1]).unwrap();
461459
assert!(!set.discard(2).unwrap());
462460
assert_eq!(1, set.len());
463461

@@ -472,7 +470,7 @@ mod tests {
472470
#[test]
473471
fn test_set_add() {
474472
Python::with_gil(|py| {
475-
let set = PySet::new(py, &[1, 2]).unwrap();
473+
let set = PySet::new_bound(py, &[1, 2]).unwrap();
476474
set.add(1).unwrap(); // Add a dupliated element
477475
assert!(set.contains(1).unwrap());
478476
});
@@ -481,21 +479,21 @@ mod tests {
481479
#[test]
482480
fn test_set_pop() {
483481
Python::with_gil(|py| {
484-
let set = PySet::new(py, &[1]).unwrap();
482+
let set = PySet::new_bound(py, &[1]).unwrap();
485483
let val = set.pop();
486484
assert!(val.is_some());
487485
let val2 = set.pop();
488486
assert!(val2.is_none());
489487
assert!(py
490-
.eval("print('Exception state should not be set.')", None, None)
488+
.eval_bound("print('Exception state should not be set.')", None, None)
491489
.is_ok());
492490
});
493491
}
494492

495493
#[test]
496494
fn test_set_iter() {
497495
Python::with_gil(|py| {
498-
let set = PySet::new(py, &[1]).unwrap();
496+
let set = PySet::new_bound(py, &[1]).unwrap();
499497

500498
for el in set {
501499
assert_eq!(1i32, el.extract::<'_, i32>().unwrap());
@@ -520,9 +518,9 @@ mod tests {
520518
#[should_panic]
521519
fn test_set_iter_mutation() {
522520
Python::with_gil(|py| {
523-
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();
521+
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
524522

525-
for _ in set {
523+
for _ in &set {
526524
let _ = set.add(42);
527525
}
528526
});
@@ -532,9 +530,9 @@ mod tests {
532530
#[should_panic]
533531
fn test_set_iter_mutation_same_len() {
534532
Python::with_gil(|py| {
535-
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();
533+
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
536534

537-
for item in set {
535+
for item in &set {
538536
let item: i32 = item.extract().unwrap();
539537
let _ = set.del_item(item);
540538
let _ = set.add(item + 10);
@@ -545,7 +543,7 @@ mod tests {
545543
#[test]
546544
fn test_set_iter_size_hint() {
547545
Python::with_gil(|py| {
548-
let set = PySet::new(py, &[1]).unwrap();
546+
let set = PySet::new_bound(py, &[1]).unwrap();
549547
let mut iter = set.iter();
550548

551549
// Exact size

0 commit comments

Comments
 (0)