Skip to content

Commit c2a3c39

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::arange
1 parent 31f1eef commit c2a3c39

File tree

6 files changed

+74
-64
lines changed

6 files changed

+74
-64
lines changed

src/array.rs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ use crate::untyped_array::{PyUntypedArray, PyUntypedArrayMethods};
8181
/// # Example
8282
///
8383
/// ```
84-
/// use numpy::PyArray;
85-
/// use ndarray::array;
84+
/// use numpy::{PyArray, PyArrayMethods};
85+
/// use ndarray::{array, Array};
8686
/// use pyo3::Python;
8787
///
8888
/// Python::with_gil(|py| {
89-
/// let pyarray = PyArray::arange(py, 0., 4., 1.).reshape([2, 2]).unwrap();
89+
/// let pyarray = PyArray::arange_bound(py, 0., 4., 1.).reshape([2, 2]).unwrap();
9090
/// let array = array![[3., 4.], [5., 6.]];
9191
///
9292
/// assert_eq!(
@@ -641,11 +641,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
641641
/// # Example
642642
///
643643
/// ```
644-
/// use numpy::PyArray;
644+
/// use numpy::{PyArray, PyArrayMethods};
645645
/// use pyo3::Python;
646646
///
647647
/// Python::with_gil(|py| {
648-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
648+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
649649
///
650650
/// assert_eq!(unsafe { *pyarray.get([1, 0, 3]).unwrap() }, 11);
651651
/// });
@@ -669,11 +669,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
669669
/// # Example
670670
///
671671
/// ```
672-
/// use numpy::PyArray;
672+
/// use numpy::{PyArray, PyArrayMethods};
673673
/// use pyo3::Python;
674674
///
675675
/// Python::with_gil(|py| {
676-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
676+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
677677
///
678678
/// unsafe {
679679
/// *pyarray.get_mut([1, 0, 3]).unwrap() = 42;
@@ -704,11 +704,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
704704
/// # Example
705705
///
706706
/// ```
707-
/// use numpy::PyArray;
707+
/// use numpy::{PyArray, PyArrayMethods};
708708
/// use pyo3::Python;
709709
///
710710
/// Python::with_gil(|py| {
711-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
711+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
712712
///
713713
/// assert_eq!(unsafe { *pyarray.uget([1, 0, 3]) }, 11);
714714
/// });
@@ -758,11 +758,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
758758
///
759759
/// # Example
760760
/// ```
761-
/// use numpy::PyArray;
761+
/// use numpy::{PyArray, PyArrayMethods};
762762
/// use pyo3::Python;
763763
///
764764
/// Python::with_gil(|py| {
765-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
765+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
766766
///
767767
/// assert_eq!(pyarray.get_owned([1, 0, 3]), Some(11));
768768
/// });
@@ -910,12 +910,12 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
910910
/// # Example
911911
///
912912
/// ```
913-
/// use numpy::PyArray;
913+
/// use numpy::{PyArray, PyArrayMethods};
914914
/// use ndarray::array;
915915
/// use pyo3::Python;
916916
///
917917
/// Python::with_gil(|py| {
918-
/// let pyarray = PyArray::arange(py, 0, 4, 1).reshape([2, 2]).unwrap();
918+
/// let pyarray = PyArray::arange_bound(py, 0, 4, 1).reshape([2, 2]).unwrap();
919919
///
920920
/// assert_eq!(
921921
/// pyarray.to_owned_array(),
@@ -1241,10 +1241,10 @@ impl<T: Element, D> PyArray<T, D> {
12411241
/// use pyo3::Python;
12421242
///
12431243
/// Python::with_gil(|py| {
1244-
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1244+
/// let pyarray_f = PyArray::arange_bound(py, 2.0, 5.0, 1.0);
12451245
/// let pyarray_i = unsafe { PyArray::<i64, _>::new_bound(py, [3], false) };
12461246
///
1247-
/// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref()).is_ok());
1247+
/// assert!(pyarray_f.copy_to(&pyarray_i).is_ok());
12481248
///
12491249
/// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
12501250
/// });
@@ -1262,11 +1262,11 @@ impl<T: Element, D> PyArray<T, D> {
12621262
/// # Example
12631263
///
12641264
/// ```
1265-
/// use numpy::PyArray;
1265+
/// use numpy::{PyArray, PyArrayMethods};
12661266
/// use pyo3::Python;
12671267
///
12681268
/// Python::with_gil(|py| {
1269-
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1269+
/// let pyarray_f = PyArray::arange_bound(py, 2.0, 5.0, 1.0);
12701270
///
12711271
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
12721272
///
@@ -1362,28 +1362,37 @@ impl<T: Element, D> PyArray<T, D> {
13621362
}
13631363

13641364
impl<T: Element + AsPrimitive<f64>> PyArray<T, Ix1> {
1365+
/// Deprecated form of [`PyArray<T, Ix1>::arange_bound`]
1366+
#[deprecated(
1367+
since = "0.21.0",
1368+
note = "will be replaced by PyArray::arange_bound in the future"
1369+
)]
1370+
pub fn arange<'py>(py: Python<'py>, start: T, stop: T, step: T) -> &Self {
1371+
Self::arange_bound(py, start, stop, step).into_gil_ref()
1372+
}
1373+
13651374
/// Return evenly spaced values within a given interval.
13661375
///
13671376
/// See [numpy.arange][numpy.arange] for the Python API and [PyArray_Arange][PyArray_Arange] for the C API.
13681377
///
13691378
/// # Example
13701379
///
13711380
/// ```
1372-
/// use numpy::PyArray;
1381+
/// use numpy::{PyArray, PyArrayMethods};
13731382
/// use pyo3::Python;
13741383
///
13751384
/// Python::with_gil(|py| {
1376-
/// let pyarray = PyArray::arange(py, 2.0, 4.0, 0.5);
1385+
/// let pyarray = PyArray::arange_bound(py, 2.0, 4.0, 0.5);
13771386
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[2.0, 2.5, 3.0, 3.5]);
13781387
///
1379-
/// let pyarray = PyArray::arange(py, -2, 4, 3);
1388+
/// let pyarray = PyArray::arange_bound(py, -2, 4, 3);
13801389
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[-2, 1]);
13811390
/// });
13821391
/// ```
13831392
///
13841393
/// [numpy.arange]: https://numpy.org/doc/stable/reference/generated/numpy.arange.html
13851394
/// [PyArray_Arange]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Arange
1386-
pub fn arange<'py>(py: Python<'py>, start: T, stop: T, step: T) -> &Self {
1395+
pub fn arange_bound<'py>(py: Python<'py>, start: T, stop: T, step: T) -> Bound<'py, Self> {
13871396
unsafe {
13881397
let ptr = PY_ARRAY_API.PyArray_Arange(
13891398
py,
@@ -1392,7 +1401,7 @@ impl<T: Element + AsPrimitive<f64>> PyArray<T, Ix1> {
13921401
step.as_(),
13931402
T::get_dtype_bound(py).num(),
13941403
);
1395-
Self::from_owned_ptr(py, ptr)
1404+
Bound::from_owned_ptr(py, ptr).downcast_into_unchecked()
13961405
}
13971406
}
13981407
}
@@ -1482,11 +1491,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
14821491
/// # Example
14831492
///
14841493
/// ```
1485-
/// use numpy::PyArray;
1494+
/// use numpy::{PyArray, PyArrayMethods};
14861495
/// use pyo3::Python;
14871496
///
14881497
/// Python::with_gil(|py| {
1489-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
1498+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
14901499
///
14911500
/// assert_eq!(unsafe { *pyarray.get([1, 0, 3]).unwrap() }, 11);
14921501
/// });
@@ -1509,11 +1518,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
15091518
/// # Example
15101519
///
15111520
/// ```
1512-
/// use numpy::PyArray;
1521+
/// use numpy::{PyArray, PyArrayMethods};
15131522
/// use pyo3::Python;
15141523
///
15151524
/// Python::with_gil(|py| {
1516-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
1525+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
15171526
///
15181527
/// unsafe {
15191528
/// *pyarray.get_mut([1, 0, 3]).unwrap() = 42;
@@ -1543,11 +1552,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
15431552
/// # Example
15441553
///
15451554
/// ```
1546-
/// use numpy::PyArray;
1555+
/// use numpy::{PyArray, PyArrayMethods};
15471556
/// use pyo3::Python;
15481557
///
15491558
/// Python::with_gil(|py| {
1550-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
1559+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
15511560
///
15521561
/// assert_eq!(unsafe { *pyarray.uget([1, 0, 3]) }, 11);
15531562
/// });
@@ -1604,11 +1613,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
16041613
///
16051614
/// # Example
16061615
/// ```
1607-
/// use numpy::PyArray;
1616+
/// use numpy::{PyArray, PyArrayMethods};
16081617
/// use pyo3::Python;
16091618
///
16101619
/// Python::with_gil(|py| {
1611-
/// let pyarray = PyArray::arange(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
1620+
/// let pyarray = PyArray::arange_bound(py, 0, 16, 1).reshape([2, 2, 4]).unwrap();
16121621
///
16131622
/// assert_eq!(pyarray.get_owned([1, 0, 3]), Some(11));
16141623
/// });
@@ -1742,12 +1751,12 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
17421751
/// # Example
17431752
///
17441753
/// ```
1745-
/// use numpy::PyArray;
1754+
/// use numpy::{PyArray, PyArrayMethods};
17461755
/// use ndarray::array;
17471756
/// use pyo3::Python;
17481757
///
17491758
/// Python::with_gil(|py| {
1750-
/// let pyarray = PyArray::arange(py, 0, 4, 1).reshape([2, 2]).unwrap();
1759+
/// let pyarray = PyArray::arange_bound(py, 0, 4, 1).reshape([2, 2]).unwrap();
17511760
///
17521761
/// assert_eq!(
17531762
/// pyarray.to_owned_array(),
@@ -1774,10 +1783,10 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
17741783
/// use pyo3::Python;
17751784
///
17761785
/// Python::with_gil(|py| {
1777-
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1786+
/// let pyarray_f = PyArray::arange_bound(py, 2.0, 5.0, 1.0);
17781787
/// let pyarray_i = unsafe { PyArray::<i64, _>::new_bound(py, [3], false) };
17791788
///
1780-
/// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref()).is_ok());
1789+
/// assert!(pyarray_f.copy_to(&pyarray_i).is_ok());
17811790
///
17821791
/// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
17831792
/// });
@@ -1795,11 +1804,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
17951804
/// # Example
17961805
///
17971806
/// ```
1798-
/// use numpy::PyArray;
1807+
/// use numpy::{PyArray, PyArrayMethods};
17991808
/// use pyo3::Python;
18001809
///
18011810
/// Python::with_gil(|py| {
1802-
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1811+
/// let pyarray_f = PyArray::arange_bound(py, 2.0, 5.0, 1.0);
18031812
///
18041813
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
18051814
///

src/borrow/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@
6262
//! The second example shows that non-overlapping and interleaved views are also supported.
6363
//!
6464
//! ```rust
65-
//! use numpy::PyArray1;
66-
//! use pyo3::{types::IntoPyDict, Python};
65+
//! use numpy::{PyArray1, PyArrayMethods};
66+
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
6767
//!
6868
//! Python::with_gil(|py| {
69-
//! let array = PyArray1::arange(py, 0.0, 10.0, 1.0);
70-
//! let locals = [("array", array)].into_py_dict(py);
69+
//! let array = PyArray1::arange_bound(py, 0.0, 10.0, 1.0);
70+
//! let locals = [("array", array)].into_py_dict_bound(py);
7171
//!
72-
//! let view1 = py.eval("array[:5]", None, Some(locals)).unwrap().downcast::<PyArray1<f64>>().unwrap();
73-
//! let view2 = py.eval("array[5:]", None, Some(locals)).unwrap().downcast::<PyArray1<f64>>().unwrap();
74-
//! let view3 = py.eval("array[::2]", None, Some(locals)).unwrap().downcast::<PyArray1<f64>>().unwrap();
75-
//! let view4 = py.eval("array[1::2]", None, Some(locals)).unwrap().downcast::<PyArray1<f64>>().unwrap();
72+
//! let view1 = py.eval_bound("array[:5]", None, Some(&locals)).unwrap().downcast_into::<PyArray1<f64>>().unwrap();
73+
//! let view2 = py.eval_bound("array[5:]", None, Some(&locals)).unwrap().downcast_into::<PyArray1<f64>>().unwrap();
74+
//! let view3 = py.eval_bound("array[::2]", None, Some(&locals)).unwrap().downcast_into::<PyArray1<f64>>().unwrap();
75+
//! let view4 = py.eval_bound("array[1::2]", None, Some(&locals)).unwrap().downcast_into::<PyArray1<f64>>().unwrap();
7676
//!
7777
//! {
7878
//! let _view1 = view1.readwrite();
@@ -562,11 +562,11 @@ where
562562
/// # Example
563563
///
564564
/// ```
565-
/// use numpy::{PyArray, PyUntypedArrayMethods};
565+
/// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods};
566566
/// use pyo3::Python;
567567
///
568568
/// Python::with_gil(|py| {
569-
/// let pyarray = PyArray::arange(py, 0, 10, 1);
569+
/// let pyarray = PyArray::arange_bound(py, 0, 10, 1);
570570
/// assert_eq!(pyarray.len(), 10);
571571
///
572572
/// let pyarray = pyarray.readwrite();

src/sum_products.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ where
171171
/// ```
172172
/// use pyo3::Python;
173173
/// use ndarray::array;
174-
/// use numpy::{einsum, pyarray, PyArray, PyArray2};
174+
/// use numpy::{einsum, pyarray, PyArray, PyArray2, PyArrayMethods};
175175
///
176176
/// Python::with_gil(|py| {
177-
/// let tensor = PyArray::arange(py, 0, 2 * 3 * 4, 1).reshape([2, 3, 4]).unwrap();
177+
/// let tensor = PyArray::arange_bound(py, 0, 2 * 3 * 4, 1).reshape([2, 3, 4]).unwrap().into_gil_ref();
178178
/// let another_tensor = pyarray![py, [20, 30], [40, 50], [60, 70]];
179179
///
180180
/// let result: &PyArray2<_> = einsum!("ijk,ji->ik", tensor, another_tensor).unwrap();

src/untyped_array.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,17 @@ impl PyUntypedArray {
122122
/// # Example
123123
///
124124
/// ```
125-
/// use numpy::PyArray1;
126-
/// use pyo3::{types::IntoPyDict, Python};
125+
/// use numpy::{PyArray1, PyUntypedArrayMethods};
126+
/// use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
127127
///
128128
/// Python::with_gil(|py| {
129-
/// let array = PyArray1::arange(py, 0, 10, 1);
129+
/// let array = PyArray1::arange_bound(py, 0, 10, 1);
130130
/// assert!(array.is_contiguous());
131131
///
132132
/// let view = py
133-
/// .eval("array[::2]", None, Some([("array", array)].into_py_dict(py)))
133+
/// .eval_bound("array[::2]", None, Some(&[("array", array)].into_py_dict_bound(py)))
134134
/// .unwrap()
135-
/// .downcast::<PyArray1<i32>>()
135+
/// .downcast_into::<PyArray1<i32>>()
136136
/// .unwrap();
137137
/// assert!(!view.is_contiguous());
138138
/// });
@@ -289,17 +289,17 @@ pub trait PyUntypedArrayMethods<'py>: Sealed {
289289
/// # Example
290290
///
291291
/// ```
292-
/// use numpy::PyArray1;
293-
/// use pyo3::{types::IntoPyDict, Python};
292+
/// use numpy::{PyArray1, PyUntypedArrayMethods};
293+
/// use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
294294
///
295295
/// Python::with_gil(|py| {
296-
/// let array = PyArray1::arange(py, 0, 10, 1);
296+
/// let array = PyArray1::arange_bound(py, 0, 10, 1);
297297
/// assert!(array.is_contiguous());
298298
///
299299
/// let view = py
300-
/// .eval("array[::2]", None, Some([("array", array)].into_py_dict(py)))
300+
/// .eval_bound("array[::2]", None, Some(&[("array", array)].into_py_dict_bound(py)))
301301
/// .unwrap()
302-
/// .downcast::<PyArray1<i32>>()
302+
/// .downcast_into::<PyArray1<i32>>()
303303
/// .unwrap();
304304
/// assert!(!view.is_contiguous());
305305
/// });

tests/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn zeros() {
129129
#[test]
130130
fn arange() {
131131
Python::with_gil(|py| {
132-
let arr = PyArray::<f64, _>::arange(py, 0.0, 1.0, 0.1);
132+
let arr = PyArray::<f64, _>::arange_bound(py, 0.0, 1.0, 0.1);
133133

134134
assert_eq!(arr.ndim(), 1);
135135
assert_eq!(arr.dims(), Dim([10]));
@@ -488,10 +488,10 @@ fn to_owned_works() {
488488
#[test]
489489
fn copy_to_works() {
490490
Python::with_gil(|py| {
491-
let arr1 = PyArray::arange(py, 2.0, 5.0, 1.0);
491+
let arr1 = PyArray::arange_bound(py, 2.0, 5.0, 1.0);
492492
let arr2 = unsafe { PyArray::<i64, _>::new_bound(py, [3], false) };
493493

494-
arr1.copy_to(arr2.as_gil_ref()).unwrap();
494+
arr1.copy_to(&arr2).unwrap();
495495

496496
assert_eq!(arr2.readonly().as_slice().unwrap(), &[2, 3, 4]);
497497
});

0 commit comments

Comments
 (0)