Skip to content

Commit c37c1a4

Browse files
Icxoluadamreichold
authored andcommitted
deprecate pyarray!
1 parent 05b1b32 commit c37c1a4

File tree

5 files changed

+97
-52
lines changed

5 files changed

+97
-52
lines changed

src/lib.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ mod doctest {
152152
#[inline(always)]
153153
fn cold() {}
154154

155+
/// Deprecated form of [`pyarray_bound`]
156+
#[deprecated(
157+
since = "0.21.0",
158+
note = "will be replace by `pyarray_bound` in the future"
159+
)]
160+
#[macro_export]
161+
macro_rules! pyarray {
162+
($py: ident, $([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*) => {{
163+
#[allow(deprecated)]
164+
$crate::IntoPyArray::into_pyarray($crate::array![$([$([$($x,)*],)*],)*], $py)
165+
}};
166+
($py: ident, $([$($x:expr),* $(,)*]),+ $(,)*) => {{
167+
#[allow(deprecated)]
168+
$crate::IntoPyArray::into_pyarray($crate::array![$([$($x,)*],)*], $py)
169+
}};
170+
($py: ident, $($x:expr),* $(,)*) => {{
171+
#[allow(deprecated)]
172+
$crate::IntoPyArray::into_pyarray($crate::array![$($x,)*], $py)
173+
}};
174+
}
175+
155176
/// Create a [`PyArray`] with one, two or three dimensions.
156177
///
157178
/// This macro is backed by [`ndarray::array`].
@@ -161,28 +182,25 @@ fn cold() {}
161182
/// ```
162183
/// use numpy::pyo3::Python;
163184
/// use numpy::ndarray::array;
164-
/// use numpy::pyarray;
185+
/// use numpy::{pyarray_bound, PyArrayMethods};
165186
///
166187
/// Python::with_gil(|py| {
167-
/// let array = pyarray![py, [1, 2], [3, 4]];
188+
/// let array = pyarray_bound![py, [1, 2], [3, 4]];
168189
///
169190
/// assert_eq!(
170191
/// array.readonly().as_array(),
171192
/// array![[1, 2], [3, 4]]
172193
/// );
173194
/// });
174195
#[macro_export]
175-
macro_rules! pyarray {
196+
macro_rules! pyarray_bound {
176197
($py: ident, $([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*) => {{
177-
#[allow(deprecated)]
178-
$crate::IntoPyArray::into_pyarray($crate::array![$([$([$($x,)*],)*],)*], $py)
198+
$crate::IntoPyArray::into_pyarray_bound($crate::array![$([$([$($x,)*],)*],)*], $py)
179199
}};
180200
($py: ident, $([$($x:expr),* $(,)*]),+ $(,)*) => {{
181-
#[allow(deprecated)]
182-
$crate::IntoPyArray::into_pyarray($crate::array![$([$($x,)*],)*], $py)
201+
$crate::IntoPyArray::into_pyarray_bound($crate::array![$([$($x,)*],)*], $py)
183202
}};
184203
($py: ident, $($x:expr),* $(,)*) => {{
185-
#[allow(deprecated)]
186-
$crate::IntoPyArray::into_pyarray($crate::array![$($x,)*], $py)
204+
$crate::IntoPyArray::into_pyarray_bound($crate::array![$($x,)*], $py)
187205
}};
188206
}

src/sum_products.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ where
5656
/// Note that this function can either return a scalar...
5757
///
5858
/// ```
59-
/// use pyo3::{Python, PyNativeType};
60-
/// use numpy::{inner_bound, pyarray, PyArray0};
59+
/// use pyo3::Python;
60+
/// use numpy::{inner_bound, pyarray_bound, PyArray0};
6161
///
6262
/// Python::with_gil(|py| {
63-
/// let vector = pyarray![py, 1.0, 2.0, 3.0].as_borrowed();
63+
/// let vector = pyarray_bound![py, 1.0, 2.0, 3.0];
6464
/// let result: f64 = inner_bound(&vector, &vector).unwrap();
6565
/// assert_eq!(result, 14.0);
6666
/// });
@@ -69,11 +69,12 @@ where
6969
/// ...or an array depending on its arguments.
7070
///
7171
/// ```
72-
/// use pyo3::{Python, Bound, PyNativeType};
73-
/// use numpy::{inner_bound, pyarray, PyArray0, PyArray0Methods};
72+
/// use pyo3::{Python, Bound};
73+
/// use numpy::prelude::*;
74+
/// use numpy::{inner_bound, pyarray_bound, PyArray0};
7475
///
7576
/// Python::with_gil(|py| {
76-
/// let vector = pyarray![py, 1, 2, 3].as_borrowed();
77+
/// let vector = pyarray_bound![py, 1, 2, 3];
7778
/// let result: Bound<'_, PyArray0<_>> = inner_bound(&vector, &vector).unwrap();
7879
/// assert_eq!(result.item(), 14);
7980
/// });
@@ -125,13 +126,13 @@ where
125126
/// Note that this function can either return an array...
126127
///
127128
/// ```
128-
/// use pyo3::{Python, Bound, PyNativeType};
129+
/// use pyo3::{Python, Bound};
129130
/// use ndarray::array;
130-
/// use numpy::{dot_bound, pyarray, PyArray2, PyArrayMethods};
131+
/// use numpy::{dot_bound, pyarray_bound, PyArray2, PyArrayMethods};
131132
///
132133
/// Python::with_gil(|py| {
133-
/// let matrix = pyarray![py, [1, 0], [0, 1]].as_borrowed();
134-
/// let another_matrix = pyarray![py, [4, 1], [2, 2]].as_borrowed();
134+
/// let matrix = pyarray_bound![py, [1, 0], [0, 1]];
135+
/// let another_matrix = pyarray_bound![py, [4, 1], [2, 2]];
135136
///
136137
/// let result: Bound<'_, PyArray2<_>> = dot_bound(&matrix, &another_matrix).unwrap();
137138
///
@@ -145,11 +146,11 @@ where
145146
/// ...or a scalar depending on its arguments.
146147
///
147148
/// ```
148-
/// use pyo3::{Python, PyNativeType};
149-
/// use numpy::{dot_bound, pyarray, PyArray0};
149+
/// use pyo3::Python;
150+
/// use numpy::{dot_bound, pyarray_bound, PyArray0};
150151
///
151152
/// Python::with_gil(|py| {
152-
/// let vector = pyarray![py, 1.0, 2.0, 3.0].as_borrowed();
153+
/// let vector = pyarray_bound![py, 1.0, 2.0, 3.0];
153154
/// let result: f64 = dot_bound(&vector, &vector).unwrap();
154155
/// assert_eq!(result, 14.0);
155156
/// });
@@ -246,13 +247,13 @@ macro_rules! einsum {
246247
/// # Example
247248
///
248249
/// ```
249-
/// use pyo3::{Python, Bound, PyNativeType};
250+
/// use pyo3::{Python, Bound};
250251
/// use ndarray::array;
251-
/// use numpy::{einsum_bound, pyarray, PyArray, PyArray2, PyArrayMethods};
252+
/// use numpy::{einsum_bound, pyarray_bound, PyArray, PyArray2, PyArrayMethods};
252253
///
253254
/// Python::with_gil(|py| {
254255
/// let tensor = PyArray::arange_bound(py, 0, 2 * 3 * 4, 1).reshape([2, 3, 4]).unwrap();
255-
/// let another_tensor = pyarray![py, [20, 30], [40, 50], [60, 70]].as_borrowed();
256+
/// let another_tensor = pyarray_bound![py, [20, 30], [40, 50], [60, 70]];
256257
///
257258
/// let result: Bound<'_, PyArray2<_>> = einsum_bound!("ijk,ji->ik", tensor, another_tensor).unwrap();
258259
///

tests/array.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use numpy::{
77
array::{PyArray0Methods, PyArrayMethods},
88
dtype_bound, get_array_module,
99
npyffi::NPY_ORDER,
10-
pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr, PyArrayDescrMethods, PyArrayDyn,
10+
pyarray_bound, PyArray, PyArray1, PyArray2, PyArrayDescr, PyArrayDescrMethods, PyArrayDyn,
1111
PyFixedString, PyFixedUnicode, PyUntypedArrayMethods, ToPyArray,
1212
};
1313
use pyo3::{
@@ -349,8 +349,8 @@ fn extract_fail_by_dtype() {
349349
#[test]
350350
fn array_cast() {
351351
Python::with_gil(|py| {
352-
let arr_f64 = pyarray![py, [1.5, 2.5, 3.5], [1.5, 2.5, 3.5]];
353-
let arr_i32: &PyArray2<i32> = arr_f64.cast(false).unwrap();
352+
let arr_f64 = pyarray_bound![py, [1.5, 2.5, 3.5], [1.5, 2.5, 3.5]];
353+
let arr_i32 = arr_f64.cast::<i32>(false).unwrap();
354354

355355
assert_eq!(arr_i32.readonly().as_array(), array![[1, 2, 3], [1, 2, 3]]);
356356
});
@@ -506,7 +506,7 @@ fn copy_to_works() {
506506
#[test]
507507
fn get_works() {
508508
Python::with_gil(|py| {
509-
let array = pyarray![py, [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]];
509+
let array = pyarray_bound![py, [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]];
510510

511511
unsafe {
512512
assert_eq!(array.get([0, 0, 0]), Some(&1));

tests/borrow.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn resize_using_exclusive_borrow() {
352352
#[test]
353353
fn matrix_from_numpy() {
354354
Python::with_gil(|py| {
355-
let array = numpy::pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
355+
let array = numpy::pyarray_bound![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
356356

357357
{
358358
let array = array.readonly();
@@ -390,7 +390,7 @@ fn matrix_from_numpy() {
390390
});
391391

392392
Python::with_gil(|py| {
393-
let array = numpy::pyarray![py, 0, 1, 2];
393+
let array = numpy::pyarray_bound![py, 0, 1, 2];
394394

395395
{
396396
let array = array.readonly();
@@ -425,7 +425,7 @@ fn matrix_from_numpy() {
425425
});
426426

427427
Python::with_gil(|py| {
428-
let array = numpy::pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
428+
let array = numpy::pyarray_bound![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
429429
let array = py
430430
.eval_bound(
431431
"a[::-1]",
@@ -443,7 +443,7 @@ fn matrix_from_numpy() {
443443
});
444444

445445
Python::with_gil(|py| {
446-
let array = numpy::pyarray![py, [[0, 1], [2, 3]], [[4, 5], [6, 7]]];
446+
let array = numpy::pyarray_bound![py, [[0, 1], [2, 3]], [[4, 5], [6, 7]]];
447447
let array = py
448448
.eval_bound(
449449
"a[:,:,0]",
@@ -467,7 +467,31 @@ fn matrix_from_numpy() {
467467
});
468468

469469
Python::with_gil(|py| {
470-
let array = numpy::pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
470+
let array = numpy::pyarray_bound![py, [[0, 1], [2, 3]], [[4, 5], [6, 7]]];
471+
let array = py
472+
.eval_bound(
473+
"a[:,:,0]",
474+
Some(&[("a", &array)].into_py_dict_bound(py)),
475+
None,
476+
)
477+
.unwrap()
478+
.downcast_into::<PyArray2<i32>>()
479+
.unwrap();
480+
let array = array.readonly();
481+
482+
let matrix: nalgebra::MatrixView<
483+
'_,
484+
i32,
485+
nalgebra::Const<2>,
486+
nalgebra::Const<2>,
487+
nalgebra::Dyn,
488+
nalgebra::Dyn,
489+
> = array.try_as_matrix().unwrap();
490+
assert_eq!(matrix, nalgebra::Matrix2::new(0, 2, 4, 6));
491+
});
492+
493+
Python::with_gil(|py| {
494+
let array = numpy::pyarray_bound![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
471495
let array = array.readonly();
472496

473497
let matrix: Option<

tests/sum_products.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
use numpy::prelude::*;
2-
use numpy::{array, dot_bound, einsum_bound, inner_bound, pyarray, PyArray0, PyArray1, PyArray2};
3-
use pyo3::{Bound, PyNativeType, Python};
2+
use numpy::{
3+
array, dot_bound, einsum_bound, inner_bound, pyarray_bound, PyArray0, PyArray1, PyArray2,
4+
};
5+
use pyo3::{Bound, Python};
46

57
#[test]
68
fn test_dot() {
79
Python::with_gil(|py| {
8-
let a = pyarray![py, [1, 0], [0, 1]].as_borrowed();
9-
let b = pyarray![py, [4, 1], [2, 2]].as_borrowed();
10+
let a = pyarray_bound![py, [1, 0], [0, 1]];
11+
let b = pyarray_bound![py, [4, 1], [2, 2]];
1012
let c: Bound<'_, PyArray2<_>> = dot_bound(&a, &b).unwrap();
1113
assert_eq!(c.readonly().as_array(), array![[4, 1], [2, 2]]);
1214

13-
let a = pyarray![py, 1, 2, 3].as_borrowed();
15+
let a = pyarray_bound![py, 1, 2, 3];
1416
let err = dot_bound::<_, _, _, Bound<'_, PyArray2<_>>>(&a, &b).unwrap_err();
1517
assert!(err.to_string().contains("not aligned"), "{}", err);
1618

17-
let a = pyarray![py, 1, 2, 3].as_borrowed();
18-
let b = pyarray![py, 0, 1, 0].as_borrowed();
19+
let a = pyarray_bound![py, 1, 2, 3];
20+
let b = pyarray_bound![py, 0, 1, 0];
1921
let c: Bound<'_, PyArray0<_>> = dot_bound(&a, &b).unwrap();
2022
assert_eq!(c.item(), 2);
2123
let c: i32 = dot_bound(&a, &b).unwrap();
2224
assert_eq!(c, 2);
2325

24-
let a = pyarray![py, 1.0, 2.0, 3.0].as_borrowed();
25-
let b = pyarray![py, 0.0, 0.0, 0.0].as_borrowed();
26+
let a = pyarray_bound![py, 1.0, 2.0, 3.0];
27+
let b = pyarray_bound![py, 0.0, 0.0, 0.0];
2628
let c: f64 = dot_bound(&a, &b).unwrap();
2729
assert_eq!(c, 0.0);
2830
});
@@ -31,24 +33,24 @@ fn test_dot() {
3133
#[test]
3234
fn test_inner() {
3335
Python::with_gil(|py| {
34-
let a = pyarray![py, 1, 2, 3].as_borrowed();
35-
let b = pyarray![py, 0, 1, 0].as_borrowed();
36+
let a = pyarray_bound![py, 1, 2, 3];
37+
let b = pyarray_bound![py, 0, 1, 0];
3638
let c: Bound<'_, PyArray0<_>> = inner_bound(&a, &b).unwrap();
3739
assert_eq!(c.item(), 2);
3840
let c: i32 = inner_bound(&a, &b).unwrap();
3941
assert_eq!(c, 2);
4042

41-
let a = pyarray![py, 1.0, 2.0, 3.0].as_borrowed();
42-
let b = pyarray![py, 0.0, 0.0, 0.0].as_borrowed();
43+
let a = pyarray_bound![py, 1.0, 2.0, 3.0];
44+
let b = pyarray_bound![py, 0.0, 0.0, 0.0];
4345
let c: f64 = inner_bound(&a, &b).unwrap();
4446
assert_eq!(c, 0.0);
4547

46-
let a = pyarray![py, [1, 0], [0, 1]].as_borrowed();
47-
let b = pyarray![py, [4, 1], [2, 2]].as_borrowed();
48+
let a = pyarray_bound![py, [1, 0], [0, 1]];
49+
let b = pyarray_bound![py, [4, 1], [2, 2]];
4850
let c: Bound<'_, PyArray2<_>> = inner_bound(&a, &b).unwrap();
4951
assert_eq!(c.readonly().as_array(), array![[4, 2], [1, 2]]);
5052

51-
let a = pyarray![py, 1, 2, 3].as_borrowed();
53+
let a = pyarray_bound![py, 1, 2, 3];
5254
let err = inner_bound::<_, _, _, Bound<'_, PyArray2<_>>>(&a, &b).unwrap_err();
5355
assert!(err.to_string().contains("not aligned"), "{}", err);
5456
});
@@ -60,8 +62,8 @@ fn test_einsum() {
6062
let a = PyArray1::<i32>::arange_bound(py, 0, 25, 1)
6163
.reshape([5, 5])
6264
.unwrap();
63-
let b = pyarray![py, 0, 1, 2, 3, 4].as_borrowed();
64-
let c = pyarray![py, [0, 1, 2], [3, 4, 5]].as_borrowed();
65+
let b = pyarray_bound![py, 0, 1, 2, 3, 4];
66+
let c = pyarray_bound![py, [0, 1, 2], [3, 4, 5]];
6567

6668
let d: Bound<'_, PyArray0<_>> = einsum_bound!("ii", a).unwrap();
6769
assert_eq!(d.item(), 60);

0 commit comments

Comments
 (0)