-
Notifications
You must be signed in to change notification settings - Fork 292
Add eitherfloat #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eitherfloat #691
Changes from all commits
8fc0231
ca38362
8ee844e
1ad6184
ceec85c
3180d8d
092be01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ use std::str::from_utf8; | |
|
||
use pyo3::prelude::*; | ||
use pyo3::types::{ | ||
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDelta, PyDict, PyFrozenSet, PyInt, PyIterator, PyList, | ||
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDelta, PyDict, PyFloat, PyFrozenSet, PyInt, PyIterator, PyList, | ||
PyMapping, PySequence, PySet, PyString, PyTime, PyTuple, PyType, | ||
}; | ||
#[cfg(not(PyPy))] | ||
|
@@ -21,8 +21,8 @@ use super::datetime::{ | |
}; | ||
use super::shared::{float_as_int, int_as_bool, map_json_err, str_as_bool, str_as_int}; | ||
use super::{ | ||
py_string_str, EitherBytes, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable, | ||
GenericIterator, GenericMapping, Input, JsonInput, PyArgs, | ||
py_string_str, EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, | ||
GenericIterable, GenericIterator, GenericMapping, Input, JsonInput, PyArgs, | ||
}; | ||
|
||
#[cfg(not(PyPy))] | ||
|
@@ -302,35 +302,40 @@ impl<'a> Input<'a> for PyAny { | |
} | ||
} | ||
|
||
fn ultra_strict_float(&self) -> ValResult<f64> { | ||
fn ultra_strict_float(&'a self) -> ValResult<EitherFloat<'a>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think with this change we might be able to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remove the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leave it for now. |
||
if self.is_instance_of::<PyInt>() { | ||
Err(ValError::new(ErrorType::FloatType, self)) | ||
} else if let Ok(float) = self.extract::<f64>() { | ||
Ok(float) | ||
} else if self.is_instance_of::<PyFloat>() { | ||
Ok(EitherFloat::Py(self)) | ||
} else { | ||
Err(ValError::new(ErrorType::FloatType, self)) | ||
} | ||
} | ||
fn strict_float(&self) -> ValResult<f64> { | ||
if let Ok(float) = self.extract::<f64>() { | ||
fn strict_float(&'a self) -> ValResult<EitherFloat<'a>> { | ||
if PyFloat::is_exact_type_of(self) { | ||
Ok(EitherFloat::Py(self)) | ||
} else if let Ok(float) = self.extract::<f64>() { | ||
// bools are cast to floats as either 0.0 or 1.0, so check for bool type in this specific case | ||
if (float == 0.0 || float == 1.0) && PyBool::is_exact_type_of(self) { | ||
Err(ValError::new(ErrorType::FloatType, self)) | ||
} else { | ||
Ok(float) | ||
Ok(EitherFloat::Py(self)) | ||
} | ||
} else { | ||
Err(ValError::new(ErrorType::FloatType, self)) | ||
} | ||
} | ||
fn lax_float(&self) -> ValResult<f64> { | ||
if let Ok(float) = self.extract::<f64>() { | ||
Ok(float) | ||
|
||
fn lax_float(&'a self) -> ValResult<EitherFloat<'a>> { | ||
if PyFloat::is_exact_type_of(self) { | ||
Ok(EitherFloat::Py(self)) | ||
} else if let Some(cow_str) = maybe_as_string(self, ErrorType::FloatParsing)? { | ||
match cow_str.as_ref().parse::<f64>() { | ||
Ok(i) => Ok(i), | ||
Ok(i) => Ok(EitherFloat::F64(i)), | ||
Err(_) => Err(ValError::new(ErrorType::FloatParsing, self)), | ||
} | ||
} else if let Ok(float) = self.extract::<f64>() { | ||
Ok(EitherFloat::F64(float)) | ||
} else { | ||
Err(ValError::new(ErrorType::FloatType, self)) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.