Skip to content

Commit

Permalink
Improve error message in Python when passing an argument without unit
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner committed Oct 23, 2024
1 parent 300f79c commit 9ded3c9
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ where
Self: PrintUnit,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
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::<PyValueError, _>(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))
Expand Down Expand Up @@ -93,9 +98,14 @@ where
Self: PrintUnit,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let (value, unit_from) = ob
.call_method0("_into_raw_parts")?
.extract::<(PyReadonlyArray<f64, D>, [i8; 7])>()?;
let Ok(raw) = ob.call_method0("_into_raw_parts") else {
return Err(PyErr::new::<PyValueError, _>(format!(
"Missing units! Expected {}, got {}.",
Self::UNIT,
ob.call_method0("__repr__")?
)));
};
let (value, unit_from) = raw.extract::<(PyReadonlyArray<f64, D>, [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 {
Expand Down

0 comments on commit 9ded3c9

Please sign in to comment.