Skip to content

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

Merged
merged 7 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ impl ErrorType {
Self::IntFromFloat => "Input should be a valid integer, got a number with a fractional part",
Self::IntParsingSize => "Unable to parse input string as an integer, exceeded maximum size",
Self::FloatType => "Input should be a valid number",
Self::FloatParsing => "Input should be a valid number, unable to parse string as an number",
Self::FloatParsing => "Input should be a valid number, unable to parse string as a number",
Self::BytesType => "Input should be a valid bytes",
Self::BytesTooShort {..} => "Data should have at least {min_length} bytes",
Self::BytesTooLong {..} => "Data should have at most {max_length} bytes",
Expand Down
10 changes: 5 additions & 5 deletions src/input/input_abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{PyMultiHostUrl, PyUrl};

use super::datetime::{EitherDate, EitherDateTime, EitherTime, EitherTimedelta};
use super::return_enums::{EitherBytes, EitherInt, EitherString};
use super::{GenericArguments, GenericIterable, GenericIterator, GenericMapping, JsonInput};
use super::{EitherFloat, GenericArguments, GenericIterable, GenericIterator, GenericMapping, JsonInput};

#[derive(Debug, Clone, Copy)]
pub enum InputType {
Expand Down Expand Up @@ -136,7 +136,7 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
self.strict_str()
}

fn validate_float(&self, strict: bool, ultra_strict: bool) -> ValResult<f64> {
fn validate_float(&'a self, strict: bool, ultra_strict: bool) -> ValResult<EitherFloat<'a>> {
if ultra_strict {
self.ultra_strict_float()
} else if strict {
Expand All @@ -145,10 +145,10 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
self.lax_float()
}
}
fn ultra_strict_float(&self) -> ValResult<f64>;
fn strict_float(&self) -> ValResult<f64>;
fn ultra_strict_float(&'a self) -> ValResult<EitherFloat<'a>>;
fn strict_float(&'a self) -> ValResult<EitherFloat<'a>>;
#[cfg_attr(has_no_coverage, no_coverage)]
fn lax_float(&self) -> ValResult<f64> {
fn lax_float(&'a self) -> ValResult<EitherFloat<'a>> {
self.strict_float()
}

Expand Down
38 changes: 19 additions & 19 deletions src/input/input_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use super::datetime::{
use super::parse_json::JsonArray;
use super::shared::{float_as_int, int_as_bool, map_json_err, str_as_bool, str_as_int};
use super::{
EitherBytes, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable, GenericIterator,
GenericMapping, Input, JsonArgs, JsonInput,
EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable,
GenericIterator, GenericMapping, Input, JsonArgs, JsonInput,
};

impl<'a> Input<'a> for JsonInput {
Expand Down Expand Up @@ -135,31 +135,31 @@ impl<'a> Input<'a> for JsonInput {
}
}

fn ultra_strict_float(&self) -> ValResult<f64> {
fn ultra_strict_float(&'a self) -> ValResult<EitherFloat<'a>> {
match self {
JsonInput::Float(f) => Ok(*f),
JsonInput::Float(f) => Ok(EitherFloat::F64(*f)),
_ => Err(ValError::new(ErrorType::FloatType, self)),
}
}
fn strict_float(&self) -> ValResult<f64> {
fn strict_float(&'a self) -> ValResult<EitherFloat<'a>> {
match self {
JsonInput::Float(f) => Ok(*f),
JsonInput::Int(i) => Ok(*i as f64),
JsonInput::Uint(u) => Ok(*u as f64),
JsonInput::Float(f) => Ok(EitherFloat::F64(*f)),
JsonInput::Int(i) => Ok(EitherFloat::F64(*i as f64)),
JsonInput::Uint(u) => Ok(EitherFloat::F64(*u as f64)),
_ => Err(ValError::new(ErrorType::FloatType, self)),
}
}
fn lax_float(&self) -> ValResult<f64> {
fn lax_float(&'a self) -> ValResult<EitherFloat<'a>> {
match self {
JsonInput::Bool(b) => match *b {
true => Ok(1.0),
false => Ok(0.0),
true => Ok(EitherFloat::F64(1.0)),
false => Ok(EitherFloat::F64(0.0)),
},
JsonInput::Float(f) => Ok(*f),
JsonInput::Int(i) => Ok(*i as f64),
JsonInput::Uint(u) => Ok(*u as f64),
JsonInput::Float(f) => Ok(EitherFloat::F64(*f)),
JsonInput::Int(i) => Ok(EitherFloat::F64(*i as f64)),
JsonInput::Uint(u) => Ok(EitherFloat::F64(*u as f64)),
JsonInput::String(str) => match str.parse::<f64>() {
Ok(i) => Ok(i),
Ok(i) => Ok(EitherFloat::F64(i)),
Err(_) => Err(ValError::new(ErrorType::FloatParsing, self)),
},
_ => Err(ValError::new(ErrorType::FloatType, self)),
Expand Down Expand Up @@ -372,16 +372,16 @@ impl<'a> Input<'a> for String {
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn ultra_strict_float(&self) -> ValResult<f64> {
fn ultra_strict_float(&'a self) -> ValResult<EitherFloat<'a>> {
self.strict_float()
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_float(&self) -> ValResult<f64> {
fn strict_float(&'a self) -> ValResult<EitherFloat<'a>> {
Err(ValError::new(ErrorType::FloatType, self))
}
fn lax_float(&self) -> ValResult<f64> {
fn lax_float(&'a self) -> ValResult<EitherFloat<'a>> {
match self.parse() {
Ok(i) => Ok(i),
Ok(f) => Ok(EitherFloat::F64(f)),
Err(_) => Err(ValError::new(ErrorType::FloatParsing, self)),
}
}
Expand Down
31 changes: 18 additions & 13 deletions src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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))]
Expand Down Expand Up @@ -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>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with this change we might be able to remove ultra_strict_float completely, maybe that's too much work for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the functionultra_strict_float, do I have to change the behavior of strict_float and lax_float?

Copy link
Member

Choose a reason for hiding this comment

The 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))
}
Expand Down
2 changes: 1 addition & 1 deletion src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) use datetime::{
pub(crate) use input_abstract::{Input, InputType};
pub(crate) use parse_json::{JsonInput, JsonObject};
pub(crate) use return_enums::{
py_string_str, AttributesGenericIterator, DictGenericIterator, EitherBytes, EitherInt, EitherString,
py_string_str, AttributesGenericIterator, DictGenericIterator, EitherBytes, EitherFloat, EitherInt, EitherString,
GenericArguments, GenericIterable, GenericIterator, GenericMapping, JsonArgs, JsonObjectGenericIterator,
MappingGenericIterator, PyArgs,
};
Expand Down
26 changes: 26 additions & 0 deletions src/input/return_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,29 @@ impl<'a> IntoPy<PyObject> for EitherInt<'a> {
}
}
}

#[cfg_attr(debug_assertions, derive(Debug))]
pub enum EitherFloat<'a> {
F64(f64),
Py(&'a PyAny),
}

impl<'a> TryInto<f64> for EitherFloat<'a> {
type Error = ValError<'a>;

fn try_into(self) -> ValResult<'a, f64> {
match self {
EitherFloat::F64(f) => Ok(f),
EitherFloat::Py(i) => i.extract().map_err(|_| ValError::new(ErrorType::FloatParsing, i)),
}
}
}

impl<'a> IntoPy<PyObject> for EitherFloat<'a> {
fn into_py(self, py: Python<'_>) -> PyObject {
match self {
Self::F64(float) => float.into_py(py),
Self::Py(float) => float.into_py(py),
}
}
}
9 changes: 6 additions & 3 deletions src/validators/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ impl Validator for FloatValidator {
_definitions: &'data Definitions<CombinedValidator>,
_recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
let float = input.validate_float(extra.strict.unwrap_or(self.strict), extra.ultra_strict)?;
let either_float = input.validate_float(extra.strict.unwrap_or(self.strict), extra.ultra_strict)?;
let float: f64 = either_float.try_into()?;
if !self.allow_inf_nan && !float.is_finite() {
return Err(ValError::new(ErrorType::FiniteNumber, input));
} else {
Ok(float.into_py(py))
}
Ok(float.into_py(py))
}

fn different_strict_behavior(
Expand Down Expand Up @@ -113,7 +115,8 @@ impl Validator for ConstrainedFloatValidator {
_definitions: &'data Definitions<CombinedValidator>,
_recursion_guard: &'s mut RecursionGuard,
) -> ValResult<'data, PyObject> {
let float = input.validate_float(extra.strict.unwrap_or(self.strict), extra.ultra_strict)?;
let either_float = input.validate_float(extra.strict.unwrap_or(self.strict), extra.ultra_strict)?;
let float: f64 = either_float.try_into()?;
if !self.allow_inf_nan && !float.is_finite() {
return Err(ValError::new(ErrorType::FiniteNumber, input));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def f(input_value, info):
('less_than', 'Input should be less than 42.1', {'lt': 42.1}),
('less_than_equal', 'Input should be less than or equal to 42.1', {'le': 42.1}),
('float_type', 'Input should be a valid number', None),
('float_parsing', 'Input should be a valid number, unable to parse string as an number', None),
('float_parsing', 'Input should be a valid number, unable to parse string as a number', None),
('bytes_type', 'Input should be a valid bytes', None),
('bytes_too_short', 'Data should have at least 42 bytes', {'min_length': 42}),
('bytes_too_long', 'Data should have at most 42 bytes', {'max_length': 42}),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_int(input_value, expected):
('"123.4"', 123.4),
('"123.0"', 123.0),
('"123"', 123.0),
('"string"', Err('Input should be a valid number, unable to parse string as an number [type=float_parsing,')),
('"string"', Err('Input should be a valid number, unable to parse string as a number [type=float_parsing,')),
],
)
def test_float(input_value, expected):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MyModel:
{
'type': 'float_parsing',
'loc': ('x',),
'msg': 'Input should be a valid number, unable to parse string as an number',
'msg': 'Input should be a valid number, unable to parse string as a number',
'input': 'x' * 60,
},
{
Expand All @@ -139,7 +139,7 @@ class MyModel:
assert repr(exc_info.value) == (
'2 validation errors for MyModel\n'
'x\n'
' Input should be a valid number, unable to parse string as an number '
' Input should be a valid number, unable to parse string as a number '
"[type=float_parsing, input_value='xxxxxxxxxxxxxxxxxxxxxxxx...xxxxxxxxxxxxxxxxxxxxxxx', input_type=str]\n"
f' For further information visit https://errors.pydantic.dev/{__version__}/v/float_parsing\n'
'y\n'
Expand Down
10 changes: 6 additions & 4 deletions tests/validators/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from ..conftest import Err, PyAndJson, plain_repr

f64_max = 1.7976931348623157e308


@pytest.mark.parametrize(
'input_value,expected',
Expand All @@ -24,7 +26,7 @@
(1e10, 1e10),
(True, 1),
(False, 0),
('wrong', Err('Input should be a valid number, unable to parse string as an number [type=float_parsing')),
('wrong', Err('Input should be a valid number, unable to parse string as a number [type=float_parsing')),
([1, 2], Err('Input should be a valid number [type=float_type, input_value=[1, 2], input_type=list]')),
],
)
Expand Down Expand Up @@ -161,7 +163,7 @@ def test_union_float_simple(py_and_json: PyAndJson):
{
'type': 'float_parsing',
'loc': ('float',),
'msg': 'Input should be a valid number, unable to parse string as an number',
'msg': 'Input should be a valid number, unable to parse string as a number',
'input': 'xxx',
},
{
Expand Down Expand Up @@ -251,15 +253,15 @@ def test_float_key(py_and_json: PyAndJson):
'pika',
True,
Err(
'Input should be a valid number, unable to parse string as an number '
'Input should be a valid number, unable to parse string as a number '
"[type=float_parsing, input_value='pika', input_type=str]"
),
),
(
'pika',
False,
Err(
'Input should be a valid number, unable to parse string as an number '
'Input should be a valid number, unable to parse string as a number '
"[type=float_parsing, input_value='pika', input_type=str]"
),
),
Expand Down