Skip to content

fix error for long int input #642

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 2 commits into from
May 26, 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 @@ -490,7 +490,7 @@ impl ErrorType {
Self::IntType => "Input should be a valid integer",
Self::IntParsing => "Input should be a valid integer, unable to parse string as an integer",
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, exceed maximum size",
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::BytesType => "Input should be a valid bytes",
Expand Down
2 changes: 1 addition & 1 deletion src/input/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn int_as_bool<'a>(input: &'a impl Input<'a>, int: i64) -> ValResult<'a, boo
pub fn str_as_int<'s, 'l>(input: &'s impl Input<'s>, str: &'l str) -> ValResult<'s, EitherInt<'s>> {
let len = str.len();
if len > 4300 {
Err(ValError::new(ErrorType::IntParsing, input))
Err(ValError::new(ErrorType::IntParsingSize, input))
} else if let Some(int) = _parse_str(input, str, len) {
Ok(int)
} else if let Some(str_stripped) = strip_decimal_zeros(str) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def f(input_value, info):
('bool_parsing', 'Input should be a valid boolean, unable to interpret input', None),
('int_type', 'Input should be a valid integer', None),
('int_parsing', 'Input should be a valid integer, unable to parse string as an integer', None),
('int_parsing_size', 'Unable to parse input string as an integer, exceed maximum size', None),
('int_parsing_size', 'Unable to parse input string as an integer, exceeded maximum size', None),
('int_from_float', 'Input should be a valid integer, got a number with a fractional part', None),
('multiple_of', 'Input should be a multiple of 42.1', {'multiple_of': 42.1}),
('greater_than', 'Input should be greater than 42.1', {'gt': 42.1}),
Expand Down
17 changes: 10 additions & 7 deletions tests/validators/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_negative_int(input_value, expected):
(
int(1e30),
Err(
'Unable to parse input string as an integer, exceed maximum size '
'Unable to parse input string as an integer, exceeded maximum size '
'[type=int_parsing_size, input_value=1e+30, input_type=float]'
),
),
Expand Down Expand Up @@ -197,7 +197,10 @@ def test_positive_json(input_value, expected):
(0, Err('Input should be less than 0 [type=less_than, input_value=0, input_type=int]')),
(-i64_max, -i64_max),
(-i64_max - 1, -i64_max - 1),
(-i64_max * 2, Err(' Unable to parse input string as an integer, exceed maximum size [type=int_parsing_size')),
(
-i64_max * 2,
Err(' Unable to parse input string as an integer, exceeded maximum size [type=int_parsing_size'),
),
],
)
def test_negative_json(input_value, expected):
Expand Down Expand Up @@ -345,18 +348,18 @@ def test_too_long():

assert exc_info.value.errors(include_url=False) == [
{
'type': 'int_parsing',
'type': 'int_parsing_size',
'loc': (),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'msg': 'Unable to parse input string as an integer, exceeded maximum size',
'input': '1' * 4301,
}
]
# insert_assert(repr(exc_info.value))
assert repr(exc_info.value) == (
"1 validation error for int\n"
" Input should be a valid integer, unable to parse string as an integer "
"[type=int_parsing, input_value='111111111111111111111111...11111111111111111111111', input_type=str]\n"
f" For further information visit https://errors.pydantic.dev/{__version__}/v/int_parsing"
" Unable to parse input string as an integer, exceeded maximum size "
"[type=int_parsing_size, input_value='111111111111111111111111...11111111111111111111111', input_type=str]\n"
f" For further information visit https://errors.pydantic.dev/{__version__}/v/int_parsing_size"
)


Expand Down