Skip to content

Commit 9895228

Browse files
committed
feature gate deprecated APIs for datetime types
1 parent f5fee94 commit 9895228

File tree

2 files changed

+62
-81
lines changed

2 files changed

+62
-81
lines changed

src/types/datetime.rs

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,10 @@ pub trait PyTimeAccess {
173173
/// Trait for accessing the components of a struct containing a tzinfo.
174174
pub trait PyTzInfoAccess<'py> {
175175
/// Deprecated form of `get_tzinfo_bound`.
176-
#[cfg_attr(
177-
not(feature = "gil-refs"),
178-
deprecated(
179-
since = "0.21.0",
180-
note = "`get_tzinfo` will be replaced by `get_tzinfo_bound` in a future PyO3 version"
181-
)
176+
#[cfg(feature = "gil-refs")]
177+
#[deprecated(
178+
since = "0.21.0",
179+
note = "`get_tzinfo` will be replaced by `get_tzinfo_bound` in a future PyO3 version"
182180
)]
183181
fn get_tzinfo(&self) -> Option<&'py PyTzInfo> {
184182
self.get_tzinfo_bound().map(Bound::into_gil_ref)
@@ -205,12 +203,10 @@ pyobject_native_type!(
205203

206204
impl PyDate {
207205
/// Deprecated form of [`PyDate::new_bound`].
208-
#[cfg_attr(
209-
not(feature = "gil-refs"),
210-
deprecated(
211-
since = "0.21.0",
212-
note = "`PyDate::new` will be replaced by `PyDate::new_bound` in a future PyO3 version"
213-
)
206+
#[cfg(feature = "gil-refs")]
207+
#[deprecated(
208+
since = "0.21.0",
209+
note = "`PyDate::new` will be replaced by `PyDate::new_bound` in a future PyO3 version"
214210
)]
215211
pub fn new(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
216212
Self::new_bound(py, year, month, day).map(Bound::into_gil_ref)
@@ -227,12 +223,10 @@ impl PyDate {
227223
}
228224

229225
/// Deprecated form of [`PyDate::from_timestamp_bound`].
230-
#[cfg_attr(
231-
not(feature = "gil-refs"),
232-
deprecated(
233-
since = "0.21.0",
234-
note = "`PyDate::from_timestamp` will be replaced by `PyDate::from_timestamp_bound` in a future PyO3 version"
235-
)
226+
#[cfg(feature = "gil-refs")]
227+
#[deprecated(
228+
since = "0.21.0",
229+
note = "`PyDate::from_timestamp` will be replaced by `PyDate::from_timestamp_bound` in a future PyO3 version"
236230
)]
237231
pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<&PyDate> {
238232
Self::from_timestamp_bound(py, timestamp).map(Bound::into_gil_ref)
@@ -296,12 +290,10 @@ pyobject_native_type!(
296290

297291
impl PyDateTime {
298292
/// Deprecated form of [`PyDateTime::new_bound`].
299-
#[cfg_attr(
300-
not(feature = "gil-refs"),
301-
deprecated(
302-
since = "0.21.0",
303-
note = "`PyDateTime::new` will be replaced by `PyDateTime::new_bound` in a future PyO3 version"
304-
)
293+
#[cfg(feature = "gil-refs")]
294+
#[deprecated(
295+
since = "0.21.0",
296+
note = "`PyDateTime::new` will be replaced by `PyDateTime::new_bound` in a future PyO3 version"
305297
)]
306298
#[allow(clippy::too_many_arguments)]
307299
pub fn new<'py>(
@@ -361,12 +353,10 @@ impl PyDateTime {
361353
}
362354

363355
/// Deprecated form of [`PyDateTime::new_bound_with_fold`].
364-
#[cfg_attr(
365-
not(feature = "gil-refs"),
366-
deprecated(
367-
since = "0.21.0",
368-
note = "`PyDateTime::new_with_fold` will be replaced by `PyDateTime::new_bound_with_fold` in a future PyO3 version"
369-
)
356+
#[cfg(feature = "gil-refs")]
357+
#[deprecated(
358+
since = "0.21.0",
359+
note = "`PyDateTime::new_with_fold` will be replaced by `PyDateTime::new_bound_with_fold` in a future PyO3 version"
370360
)]
371361
#[allow(clippy::too_many_arguments)]
372362
pub fn new_with_fold<'py>(
@@ -436,12 +426,10 @@ impl PyDateTime {
436426
}
437427

438428
/// Deprecated form of [`PyDateTime::from_timestamp_bound`].
439-
#[cfg_attr(
440-
not(feature = "gil-refs"),
441-
deprecated(
442-
since = "0.21.0",
443-
note = "`PyDateTime::from_timestamp` will be replaced by `PyDateTime::from_timestamp_bound` in a future PyO3 version"
444-
)
429+
#[cfg(feature = "gil-refs")]
430+
#[deprecated(
431+
since = "0.21.0",
432+
note = "`PyDateTime::from_timestamp` will be replaced by `PyDateTime::from_timestamp_bound` in a future PyO3 version"
445433
)]
446434
pub fn from_timestamp<'py>(
447435
py: Python<'py>,
@@ -598,12 +586,10 @@ pyobject_native_type!(
598586

599587
impl PyTime {
600588
/// Deprecated form of [`PyTime::new_bound`].
601-
#[cfg_attr(
602-
not(feature = "gil-refs"),
603-
deprecated(
604-
since = "0.21.0",
605-
note = "`PyTime::new` will be replaced by `PyTime::new_bound` in a future PyO3 version"
606-
)
589+
#[cfg(feature = "gil-refs")]
590+
#[deprecated(
591+
since = "0.21.0",
592+
note = "`PyTime::new` will be replaced by `PyTime::new_bound` in a future PyO3 version"
607593
)]
608594
pub fn new<'py>(
609595
py: Python<'py>,
@@ -649,12 +635,10 @@ impl PyTime {
649635
}
650636

651637
/// Deprecated form of [`PyTime::new_bound_with_fold`].
652-
#[cfg_attr(
653-
not(feature = "gil-refs"),
654-
deprecated(
655-
since = "0.21.0",
656-
note = "`PyTime::new_with_fold` will be replaced by `PyTime::new_bound_with_fold` in a future PyO3 version"
657-
)
638+
#[cfg(feature = "gil-refs")]
639+
#[deprecated(
640+
since = "0.21.0",
641+
note = "`PyTime::new_with_fold` will be replaced by `PyTime::new_bound_with_fold` in a future PyO3 version"
658642
)]
659643
pub fn new_with_fold<'py>(
660644
py: Python<'py>,
@@ -804,12 +788,10 @@ pyobject_native_type!(
804788
);
805789

806790
/// Deprecated form of [`timezone_utc_bound`].
807-
#[cfg_attr(
808-
not(feature = "gil-refs"),
809-
deprecated(
810-
since = "0.21.0",
811-
note = "`timezone_utc` will be replaced by `timezone_utc_bound` in a future PyO3 version"
812-
)
791+
#[cfg(feature = "gil-refs")]
792+
#[deprecated(
793+
since = "0.21.0",
794+
note = "`timezone_utc` will be replaced by `timezone_utc_bound` in a future PyO3 version"
813795
)]
814796
pub fn timezone_utc(py: Python<'_>) -> &PyTzInfo {
815797
timezone_utc_bound(py).into_gil_ref()
@@ -858,12 +840,10 @@ pyobject_native_type!(
858840

859841
impl PyDelta {
860842
/// Deprecated form of [`PyDelta::new_bound`].
861-
#[cfg_attr(
862-
not(feature = "gil-refs"),
863-
deprecated(
864-
since = "0.21.0",
865-
note = "`PyDelta::new` will be replaced by `PyDelta::new_bound` in a future PyO3 version"
866-
)
843+
#[cfg(feature = "gil-refs")]
844+
#[deprecated(
845+
since = "0.21.0",
846+
note = "`PyDelta::new` will be replaced by `PyDelta::new_bound` in a future PyO3 version"
867847
)]
868848
pub fn new(
869849
py: Python<'_>,
@@ -936,7 +916,6 @@ fn opt_to_pyobj(opt: Option<&Bound<'_, PyTzInfo>>) -> *mut ffi::PyObject {
936916
}
937917

938918
#[cfg(test)]
939-
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
940919
mod tests {
941920
use super::*;
942921
#[cfg(feature = "macros")]
@@ -947,14 +926,15 @@ mod tests {
947926
#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons
948927
fn test_datetime_fromtimestamp() {
949928
Python::with_gil(|py| {
950-
let dt = PyDateTime::from_timestamp(py, 100.0, None).unwrap();
929+
let dt = PyDateTime::from_timestamp_bound(py, 100.0, None).unwrap();
951930
py_run!(
952931
py,
953932
dt,
954933
"import datetime; assert dt == datetime.datetime.fromtimestamp(100)"
955934
);
956935

957-
let dt = PyDateTime::from_timestamp(py, 100.0, Some(timezone_utc(py))).unwrap();
936+
let dt =
937+
PyDateTime::from_timestamp_bound(py, 100.0, Some(&timezone_utc_bound(py))).unwrap();
958938
py_run!(
959939
py,
960940
dt,
@@ -968,7 +948,7 @@ mod tests {
968948
#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons
969949
fn test_date_fromtimestamp() {
970950
Python::with_gil(|py| {
971-
let dt = PyDate::from_timestamp(py, 100).unwrap();
951+
let dt = PyDate::from_timestamp_bound(py, 100).unwrap();
972952
py_run!(
973953
py,
974954
dt,
@@ -981,8 +961,10 @@ mod tests {
981961
#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons
982962
fn test_new_with_fold() {
983963
Python::with_gil(|py| {
984-
let a = PyDateTime::new_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, false);
985-
let b = PyDateTime::new_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, true);
964+
let a =
965+
PyDateTime::new_bound_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, false);
966+
let b =
967+
PyDateTime::new_bound_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, true);
986968

987969
assert!(!a.unwrap().get_fold());
988970
assert!(b.unwrap().get_fold());
@@ -991,26 +973,25 @@ mod tests {
991973

992974
#[test]
993975
#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons
994-
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
995976
fn test_get_tzinfo() {
996977
crate::Python::with_gil(|py| {
997-
let utc = timezone_utc(py);
978+
let utc = timezone_utc_bound(py);
998979

999-
let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, Some(utc)).unwrap();
980+
let dt = PyDateTime::new_bound(py, 2018, 1, 1, 0, 0, 0, 0, Some(&utc)).unwrap();
1000981

1001-
assert!(dt.get_tzinfo().unwrap().eq(utc).unwrap());
982+
assert!(dt.get_tzinfo_bound().unwrap().eq(&utc).unwrap());
1002983

1003-
let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, None).unwrap();
984+
let dt = PyDateTime::new_bound(py, 2018, 1, 1, 0, 0, 0, 0, None).unwrap();
1004985

1005-
assert!(dt.get_tzinfo().is_none());
986+
assert!(dt.get_tzinfo_bound().is_none());
1006987

1007-
let t = PyTime::new(py, 0, 0, 0, 0, Some(utc)).unwrap();
988+
let t = PyTime::new_bound(py, 0, 0, 0, 0, Some(&utc)).unwrap();
1008989

1009-
assert!(t.get_tzinfo().unwrap().eq(utc).unwrap());
990+
assert!(t.get_tzinfo_bound().unwrap().eq(utc).unwrap());
1010991

1011-
let t = PyTime::new(py, 0, 0, 0, 0, None).unwrap();
992+
let t = PyTime::new_bound(py, 0, 0, 0, 0, None).unwrap();
1012993

1013-
assert!(t.get_tzinfo().is_none());
994+
assert!(t.get_tzinfo_bound().is_none());
1014995
});
1015996
}
1016997

@@ -1024,9 +1005,9 @@ mod tests {
10241005
.unwrap()
10251006
.call_method1("utcoffset", ((),))
10261007
.unwrap()
1027-
.extract::<&PyDelta>()
1008+
.downcast_into::<PyDelta>()
10281009
.unwrap()
1029-
.eq(PyDelta::new(py, 0, -3600, 0, true).unwrap())
1010+
.eq(PyDelta::new_bound(py, 0, -3600, 0, true).unwrap())
10301011
.unwrap()
10311012
);
10321013

@@ -1035,9 +1016,9 @@ mod tests {
10351016
.unwrap()
10361017
.call_method1("utcoffset", ((),))
10371018
.unwrap()
1038-
.extract::<&PyDelta>()
1019+
.downcast_into::<PyDelta>()
10391020
.unwrap()
1040-
.eq(PyDelta::new(py, 0, 3600, 0, true).unwrap())
1021+
.eq(PyDelta::new_bound(py, 0, 3600, 0, true).unwrap())
10411022
.unwrap()
10421023
);
10431024

src/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use self::capsule::{PyCapsule, PyCapsuleMethods};
99
pub use self::code::PyCode;
1010
pub use self::complex::{PyComplex, PyComplexMethods};
1111
#[allow(deprecated)]
12-
#[cfg(not(Py_LIMITED_API))]
12+
#[cfg(all(not(Py_LIMITED_API), feature = "gil-refs"))]
1313
pub use self::datetime::timezone_utc;
1414
#[cfg(not(Py_LIMITED_API))]
1515
pub use self::datetime::{

0 commit comments

Comments
 (0)