Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
- v0.28.0
- Fix mismatched behavior between `PyArrayLike1` and `PyArrayLike2` when used with floats ([#520](https://github.com/PyO3/rust-numpy/pull/520))

- v0.27.1
- Bump ndarray dependency to v0.17. ([#516](https://github.com/PyO3/rust-numpy/pull/516))

Expand Down
16 changes: 10 additions & 6 deletions src/array_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use pyo3::{
};

use crate::array::PyArrayMethods;
use crate::{get_array_module, Element, IntoPyArray, PyArray, PyReadonlyArray};
use crate::{get_array_module, Element, IntoPyArray, PyArray, PyReadonlyArray, PyUntypedArray};

pub trait Coerce: Sealed {
const VAL: bool;
const ALLOW_TYPE_CHANGE: bool;
}

mod sealed {
Expand All @@ -29,7 +29,7 @@ pub struct TypeMustMatch;
impl Sealed for TypeMustMatch {}

impl Coerce for TypeMustMatch {
const VAL: bool = false;
const ALLOW_TYPE_CHANGE: bool = false;
}

/// Marker type to indicate that the element type received via [`PyArrayLike`] can be cast to the specified type by NumPy's [`asarray`](https://numpy.org/doc/stable/reference/generated/numpy.asarray.html).
Expand All @@ -39,7 +39,7 @@ pub struct AllowTypeChange;
impl Sealed for AllowTypeChange {}

impl Coerce for AllowTypeChange {
const VAL: bool = true;
const ALLOW_TYPE_CHANGE: bool = true;
}

/// Receiver for arrays or array-like types.
Expand Down Expand Up @@ -151,7 +151,11 @@ where

let py = ob.py();

if matches!(D::NDIM, None | Some(1)) {
// If the input is already an ndarray and `TypeMustMatch` is used then no type conversion
// should be performed.
if (C::ALLOW_TYPE_CHANGE || ob.cast::<PyUntypedArray>().is_err())
&& matches!(D::NDIM, None | Some(1))
{
if let Ok(vec) = ob.extract::<Vec<T>>() {
let array = Array1::from(vec)
.into_dimensionality()
Expand All @@ -170,7 +174,7 @@ where
})?
.bind(py);

let kwargs = if C::VAL {
let kwargs = if C::ALLOW_TYPE_CHANGE {
let kwargs = PyDict::new(py);
kwargs.set_item(intern!(py, "dtype"), T::get_dtype(py))?;
Some(kwargs)
Expand Down
44 changes: 44 additions & 0 deletions tests/array_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,50 @@ fn unsafe_cast_shall_fail() {
});
}

#[test]
fn extract_1d_array_of_different_float_types_fail() {
Python::attach(|py| {
let locals = get_np_locals(py);
let py_list = py
.eval(
c_str!("np.array([1, 2, 3, 4], dtype='float64')"),
Some(&locals),
None,
)
.unwrap();
let extracted_array_f32 = py_list.extract::<PyArrayLike1<'_, f32>>();
let extracted_array_f64 = py_list.extract::<PyArrayLike1<'_, f64>>().unwrap();

assert!(extracted_array_f32.is_err());
assert_eq!(
array![1_f64, 2_f64, 3_f64, 4_f64],
extracted_array_f64.as_array()
);
});
}

#[test]
fn extract_2d_array_of_different_float_types_fail() {
Python::attach(|py| {
let locals = get_np_locals(py);
let py_list = py
.eval(
c_str!("np.array([[1, 2], [3, 4]], dtype='float64')"),
Some(&locals),
None,
)
.unwrap();
let extracted_array_f32 = py_list.extract::<PyArrayLike2<'_, f32>>();
let extracted_array_f64 = py_list.extract::<PyArrayLike2<'_, f64>>().unwrap();

assert!(extracted_array_f32.is_err());
assert_eq!(
array![[1_f64, 2_f64], [3_f64, 4_f64]],
extracted_array_f64.as_array()
);
});
}

#[test]
fn unsafe_cast_with_coerce_works() {
Python::attach(|py| {
Expand Down