Skip to content

Commit 4dd37aa

Browse files
committed
Avoid unexpected conversion with PyArrayLike1
1 parent 06d45ed commit 4dd37aa

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/array_like.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use pyo3::{
1010
};
1111

1212
use crate::array::PyArrayMethods;
13-
use crate::{get_array_module, Element, IntoPyArray, PyArray, PyReadonlyArray};
13+
use crate::{get_array_module, Element, IntoPyArray, PyArray, PyReadonlyArray, PyUntypedArray};
1414

1515
pub trait Coerce: Sealed {
1616
const VAL: bool;
@@ -151,7 +151,9 @@ where
151151

152152
let py = ob.py();
153153

154-
if matches!(D::NDIM, None | Some(1)) {
154+
// If the input is already an ndarray and `TypeMustMatch` is used then no type conversion
155+
// should be performed.
156+
if (C::VAL || ob.cast::<PyUntypedArray>().is_err()) && matches!(D::NDIM, None | Some(1)) {
155157
if let Ok(vec) = ob.extract::<Vec<T>>() {
156158
let array = Array1::from(vec)
157159
.into_dimensionality()

0 commit comments

Comments
 (0)