Skip to content

Commit 3e30540

Browse files
committed
allow exact floats to int fields, fix #84
1 parent eb869bc commit 3e30540

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/input/input_json.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ impl<'a> Input<'a> for JsonInput {
5656
JsonInput::Bool(b) => Ok(*b),
5757
JsonInput::String(s) => str_as_bool(self, s),
5858
JsonInput::Int(int) => int_as_bool(self, *int),
59-
// TODO float??
59+
JsonInput::Float(float) => match float_as_int(self, *float) {
60+
Ok(int) => int_as_bool(self, int),
61+
_ => Err(ValError::new(ErrorKind::BoolType, self)),
62+
},
6063
_ => Err(ValError::new(ErrorKind::BoolType, self)),
6164
}
6265
}

src/input/input_python.rs

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ impl<'a> Input<'a> for PyAny {
9696
str_as_bool(self, &either_str.as_cow())
9797
} else if let Ok(int) = self.extract::<i64>() {
9898
int_as_bool(self, int)
99+
} else if let Ok(float) = self.extract::<f64>() {
100+
match float_as_int(self, float) {
101+
Ok(int) => int_as_bool(self, int),
102+
_ => Err(ValError::new(ErrorKind::BoolType, self)),
103+
}
99104
} else {
100105
Err(ValError::new(ErrorKind::BoolType, self))
101106
}

tests/validators/test_bool.py

+5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
(False, False),
1414
(True, True),
1515
(0, False),
16+
(0.0, False),
1617
(1, True),
18+
(1.0, True),
1719
('yes', True),
1820
('no', False),
1921
('true', True),
@@ -27,6 +29,9 @@
2729
),
2830
(2, Err('Value must be a valid boolean, unable to interpret input [kind=bool_parsing, input_value=2')),
2931
([], Err('Value must be a valid boolean [kind=bool_type, input_value=[], input_type=list]')),
32+
(1.1, Err('Value must be a valid boolean [kind=bool_type, input_value=1.1, input_type=float]')),
33+
(2, Err('unable to interpret input [kind=bool_parsing, input_value=2, input_type=int]')),
34+
(2.0, Err('unable to interpret input [kind=bool_parsing, input_value=2.0, input_type=float]')),
3035
],
3136
)
3237
def test_bool(py_or_json, input_value, expected):

tests/validators/test_float.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@ def test_float(py_or_json, input_value, expected):
4545
(42, 42),
4646
(42.0, 42.0),
4747
(42.5, 42.5),
48-
pytest.param(
49-
'42', Err("Value must be a valid number [kind=float_type, input_value='42', input_type=str]"), id='string'
50-
),
51-
pytest.param(
52-
True, Err('Value must be a valid number [kind=float_type, input_value=True, input_type=bool]'), id='bool'
53-
),
48+
('42', Err("Value must be a valid number [kind=float_type, input_value='42', input_type=str]")),
49+
(True, Err('Value must be a valid number [kind=float_type, input_value=True, input_type=bool]')),
5450
],
51+
ids=repr,
5552
)
5653
def test_float_strict(py_or_json, input_value, expected):
5754
v = py_or_json({'type': 'float', 'strict': True})

0 commit comments

Comments
 (0)