Skip to content

Commit b70b330

Browse files
committed
simplify errors
1 parent 772a549 commit b70b330

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/input/input_json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
171171
create_decimal(&PyString::new_bound(py, &f.to_string()), self).map(ValidationMatch::strict)
172172
}
173173
JsonValue::Str(..) | JsonValue::Int(..) | JsonValue::BigInt(..) => {
174-
create_decimal(self.to_object(py).bind(py), self).map(ValidationMatch::lax)
174+
create_decimal(self.to_object(py).bind(py), self).map(ValidationMatch::strict)
175175
}
176176
_ => Err(ValError::new(ErrorTypeDefaults::DecimalType, self)),
177177
}

src/input/input_python.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,33 +312,39 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
312312

313313
// Fast path for existing decimal objects
314314
if self.is_exact_instance(decimal_type) {
315-
Ok(ValidationMatch::exact(self.to_owned().clone()))
316-
} else if self.is_instance(decimal_type)? {
317-
// Upcast subclasses to decimal
318-
create_decimal(self, self).map(ValidationMatch::strict)
319-
} else {
320-
if strict {
321-
return Err(ValError::new(
322-
ErrorType::IsInstanceOf {
323-
class: decimal_type
324-
.qualname()
325-
.and_then(|name| name.extract())
326-
.unwrap_or_else(|_| "Decimal".to_owned()),
327-
context: None,
328-
},
329-
self,
330-
));
331-
}
315+
return Ok(ValidationMatch::exact(self.to_owned().clone()));
316+
}
317+
318+
if !strict {
332319
if self.is_instance_of::<PyString>() || (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>())
333320
{
334321
// Checking isinstance for str / int / bool is fast compared to decimal / float
335-
create_decimal(self, self).map(ValidationMatch::lax)
336-
} else if self.is_instance_of::<PyFloat>() {
337-
create_decimal(self.str()?.as_any(), self).map(ValidationMatch::lax)
338-
} else {
339-
Err(ValError::new(ErrorTypeDefaults::DecimalType, self))
322+
return create_decimal(self, self).map(ValidationMatch::lax);
340323
}
324+
325+
if self.is_instance_of::<PyFloat>() {
326+
return create_decimal(self.str()?.as_any(), self).map(ValidationMatch::lax);
327+
}
328+
}
329+
330+
if self.is_instance(decimal_type)? {
331+
// Upcast subclasses to decimal
332+
return create_decimal(self, self).map(ValidationMatch::strict);
341333
}
334+
335+
let error_type = if strict {
336+
ErrorType::IsInstanceOf {
337+
class: decimal_type
338+
.qualname()
339+
.and_then(|name| name.extract())
340+
.unwrap_or_else(|_| "Decimal".to_owned()),
341+
context: None,
342+
}
343+
} else {
344+
ErrorTypeDefaults::DecimalType
345+
};
346+
347+
Err(ValError::new(error_type, self))
342348
}
343349

344350
type Dict<'a> = GenericPyMapping<'a, 'py> where Self: 'a;

0 commit comments

Comments
 (0)