Skip to content

Commit e8f1385

Browse files
committed
remove unnecessary code parts
1 parent 2c710aa commit e8f1385

File tree

4 files changed

+10
-23
lines changed

4 files changed

+10
-23
lines changed

src/input/input_python.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -343,29 +343,16 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
343343
return Ok(ValidationMatch::exact(self.to_owned().clone()));
344344
}
345345

346-
if !strict {
347-
if self.is_instance_of::<PyString>() || (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>())
348-
{
349-
// Checking isinstance for str / int / bool is fast compared to decimal / float
350-
return create_fraction(self, self).map(ValidationMatch::lax);
351-
}
352-
353-
if self.is_instance_of::<PyFloat>() {
354-
return create_fraction(self.str()?.as_any(), self).map(ValidationMatch::lax);
355-
}
356-
}
357-
358-
if self.is_instance(fraction_type)? {
359-
// Upcast subclasses to decimal
360-
return create_decimal(self, self).map(ValidationMatch::strict);
346+
if !strict && self.is_instance_of::<PyString>() {
347+
return create_fraction(self, self).map(ValidationMatch::lax);
361348
}
362349

363350
let error_type = if strict {
364351
ErrorType::IsInstanceOf {
365352
class: fraction_type
366353
.qualname()
367354
.and_then(|name| name.extract())
368-
.unwrap_or_else(|_| "Decimal".to_owned()),
355+
.unwrap_or_else(|_| "Fraction".to_owned()),
369356
context: None,
370357
}
371358
} else {

src/input/shared.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub fn fraction_as_int<'py>(
234234
) -> ValResult<EitherInt<'py>> {
235235
let py = fraction.py();
236236

237+
// as_integer_ratio was added in Python 3.8, so this should be fine
237238
let (numerator, denominator) = fraction
238239
.call_method0(intern!(py, "as_integer_ratio"))?
239240
.extract::<(Bound<'_, PyAny>, Bound<'_, PyAny>)>()?;

src/serializers/ob_type.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ pub enum IsType {
6363

6464
impl ObTypeLookup {
6565
fn new(py: Python) -> Self {
66-
// todo: delete before PR ready
6766
Self {
6867
none: PyNone::type_object_raw(py) as usize,
6968
int: PyInt::type_object_raw(py) as usize,

src/validators/fraction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use pyo3::exceptions::{PyTypeError, PyValueError, PyZeroDivisionError};
3+
use pyo3::exceptions::{PyTypeError, PyValueError};
44
use pyo3::intern;
55
use pyo3::sync::PyOnceLock;
66
use pyo3::types::{PyDict, PyString, PyType};
@@ -37,7 +37,7 @@ fn validate_as_fraction(
3737
Some(value) => match value.validate_fraction(false, py) {
3838
Ok(v) => Ok(Some(v.into_inner().unbind())),
3939
Err(_) => Err(PyValueError::new_err(format!(
40-
"'{key}' must be coercible to a Decimal instance",
40+
"'{key}' must be coercible to a Fraction instance",
4141
))),
4242
},
4343
None => Ok(None),
@@ -147,13 +147,13 @@ impl Validator for FractionValidator {
147147

148148
pub(crate) fn create_fraction<'py>(arg: &Bound<'py, PyAny>, input: impl ToErrorValue) -> ValResult<Bound<'py, PyAny>> {
149149
let py = arg.py();
150-
get_fraction_type(py).call1((arg,)).map_err(|e| {
151-
handle_fraction_new_error(input, e)
152-
})
150+
get_fraction_type(py)
151+
.call1((arg,))
152+
.map_err(|e| handle_fraction_new_error(input, e))
153153
}
154154

155155
fn handle_fraction_new_error(input: impl ToErrorValue, error: PyErr) -> ValError {
156-
Python::with_gil(|py| {
156+
Python::attach(|py| {
157157
if error.matches(py, PyValueError::type_object(py)).unwrap_or(false) {
158158
ValError::new(ErrorTypeDefaults::FractionParsing, input)
159159
} else if error.matches(py, PyTypeError::type_object(py)).unwrap_or(false) {

0 commit comments

Comments
 (0)