diff --git a/src/python.rs b/src/python.rs index f52d6a0..f620f31 100644 --- a/src/python.rs +++ b/src/python.rs @@ -62,9 +62,14 @@ where Self: PrintUnit, { fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - let (value, unit_from) = ob - .call_method0("_into_raw_parts")? - .extract::<(f64, [i8; 7])>()?; + let Ok(raw) = ob.call_method0("_into_raw_parts") else { + return Err(PyErr::new::(format!( + "Missing units! Expected {}, got {}.", + Self::UNIT, + ob.call_method0("__repr__")? + ))); + }; + let (value, unit_from) = raw.extract::<(f64, [i8; 7])>()?; let unit_into = [L::I8, M::I8, T::I8, I::I8, N::I8, THETA::I8, J::I8]; if unit_into == unit_from { Ok(Quantity(value, PhantomData)) @@ -93,9 +98,14 @@ where Self: PrintUnit, { fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - let (value, unit_from) = ob - .call_method0("_into_raw_parts")? - .extract::<(PyReadonlyArray, [i8; 7])>()?; + let Ok(raw) = ob.call_method0("_into_raw_parts") else { + return Err(PyErr::new::(format!( + "Missing units! Expected {}, got {}.", + Self::UNIT, + ob.call_method0("__repr__")? + ))); + }; + let (value, unit_from) = raw.extract::<(PyReadonlyArray, [i8; 7])>()?; let value = value.as_array().to_owned(); let unit_into = [L::I8, M::I8, T::I8, I::I8, N::I8, THETA::I8, J::I8]; if unit_into == unit_from {