Skip to content

Commit 3ce535a

Browse files
jakemoranIcxolu
authored andcommitted
Remove extraction from Vec for PyArrayLikeDyn
1 parent 924e0d2 commit 3ce535a

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add support for free-threaded stable abi (abi3t) from Python 3.15t+ ([#556](https://github.com/PyO3/rust-numpy/pull/556))
66
- fixed free-threaded builds for 32 bit platforms from Python 3.15+ ([#556](https://github.com/PyO3/rust-numpy/pull/556))
77
- Drop support for Python 3.8 ([#567](https://github.com/PyO3/rust-numpy/pull/567))
8+
- fix accidental removal of singleton dimensions when extracting `PyArrayLikeDyn<'_, T, AllowTypeChange>` ([#496](https://github.com/PyO3/rust-numpy/pull/496))
89

910
- v0.29.0
1011
- Fix PyArray_DTypeMeta definition when Py_LIMITED_API is disabled ([#532](https://github.com/PyO3/rust-numpy/pull/532))

src/array_like.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Coerce for AllowTypeChange {
8787
/// let np = get_array_module(py).unwrap();
8888
/// let sum_up = wrap_pyfunction!(sum_up)(py).unwrap();
8989
///
90-
/// py_run!(py, np sum_up, r"assert sum_up((1., 2., 3.)) == 6");
90+
/// py_run!(py, np sum_up, r"assert sum_up(np.array([1., 2., 3.])) == 6");
9191
/// });
9292
/// ```
9393
///
@@ -107,6 +107,7 @@ impl Coerce for AllowTypeChange {
107107
/// let np = get_array_module(py).unwrap();
108108
/// let sum_up = wrap_pyfunction!(sum_up)(py).unwrap();
109109
///
110+
/// py_run!(py, np sum_up, r"assert sum_up(np.array([1.5, 2.5])) == 3");
110111
/// py_run!(py, np sum_up, r"assert sum_up((1.5, 2.5)) == 3");
111112
/// });
112113
/// ```
@@ -150,7 +151,7 @@ where
150151
// If the input is already an ndarray and `TypeMustMatch` is used then no type conversion
151152
// should be performed.
152153
if (C::ALLOW_TYPE_CHANGE || ob.cast::<PyUntypedArray>().is_err())
153-
&& matches!(D::NDIM, None | Some(1))
154+
&& matches!(D::NDIM, Some(1))
154155
{
155156
if let Ok(vec) = ob.extract::<Vec<T>>() {
156157
let array = Array1::from(vec)
@@ -162,7 +163,7 @@ where
162163
}
163164
}
164165

165-
let (dtype, flags) = if C::ALLOW_TYPE_CHANGE {
166+
let (dtype, flags) = if C::ALLOW_TYPE_CHANGE || ob.cast::<PyUntypedArray>().is_err() {
166167
(Some(T::get_dtype(py)), NPY_ARRAY_FORCECAST)
167168
} else {
168169
(None, 0)

tests/array_like.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use ndarray::array;
2-
use numpy::{get_array_module, AllowTypeChange, PyArrayLike1, PyArrayLike2, PyArrayLikeDyn};
2+
use numpy::{
3+
get_array_module, AllowTypeChange, PyArrayLike1, PyArrayLike2, PyArrayLikeDyn,
4+
PyUntypedArrayMethods as _,
5+
};
36
use pyo3::{
47
ffi::c_str,
58
types::{IntoPyDict, PyAnyMethods, PyDict},
@@ -115,6 +118,25 @@ fn convert_1d_list_on_extract() {
115118
});
116119
}
117120

121+
#[test]
122+
fn preserve_trailing_singleton_dims() {
123+
Python::attach(|py| {
124+
let locals = get_np_locals(py);
125+
let py_array = py
126+
.eval(
127+
c_str!("np.array([[1], [2], [3]], dtype='int32')"),
128+
Some(&locals),
129+
None,
130+
)
131+
.unwrap();
132+
let extracted_array = py_array
133+
.extract::<PyArrayLikeDyn<'_, f64, AllowTypeChange>>()
134+
.unwrap();
135+
136+
assert_eq!(extracted_array.shape(), &[3, 1]);
137+
})
138+
}
139+
118140
#[test]
119141
fn unsafe_cast_shall_fail() {
120142
Python::attach(|py| {

0 commit comments

Comments
 (0)