Skip to content

Commit 06d45ed

Browse files
committed
Extracting as PyArrayLike1 with TypeMustMatch fails when source and destination types doesn't match
1 parent 78d5e8d commit 06d45ed

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/array_like.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,50 @@ fn unsafe_cast_shall_fail() {
132132
});
133133
}
134134

135+
#[test]
136+
fn extract_1d_array_of_different_float_types_fail() {
137+
Python::attach(|py| {
138+
let locals = get_np_locals(py);
139+
let py_list = py
140+
.eval(
141+
c_str!("np.array([1, 2, 3, 4], dtype='float64')"),
142+
Some(&locals),
143+
None,
144+
)
145+
.unwrap();
146+
let extracted_array_f32 = py_list.extract::<PyArrayLike1<'_, f32>>();
147+
let extracted_array_f64 = py_list.extract::<PyArrayLike1<'_, f64>>().unwrap();
148+
149+
assert!(extracted_array_f32.is_err());
150+
assert_eq!(
151+
array![1_f64, 2_f64, 3_f64, 4_f64],
152+
extracted_array_f64.as_array()
153+
);
154+
});
155+
}
156+
157+
#[test]
158+
fn extract_2d_array_of_different_float_types_fail() {
159+
Python::attach(|py| {
160+
let locals = get_np_locals(py);
161+
let py_list = py
162+
.eval(
163+
c_str!("np.array([[1, 2], [3, 4]], dtype='float64')"),
164+
Some(&locals),
165+
None,
166+
)
167+
.unwrap();
168+
let extracted_array_f32 = py_list.extract::<PyArrayLike2<'_, f32>>();
169+
let extracted_array_f64 = py_list.extract::<PyArrayLike2<'_, f64>>().unwrap();
170+
171+
assert!(extracted_array_f32.is_err());
172+
assert_eq!(
173+
array![[1_f64, 2_f64], [3_f64, 4_f64]],
174+
extracted_array_f64.as_array()
175+
);
176+
});
177+
}
178+
135179
#[test]
136180
fn unsafe_cast_with_coerce_works() {
137181
Python::attach(|py| {

0 commit comments

Comments
 (0)