Skip to content

Commit e2c0184

Browse files
committed
fix big int parsing problems from str
1 parent 6a3c51e commit e2c0184

File tree

3 files changed

+5
-27
lines changed

3 files changed

+5
-27
lines changed

src/input/input_json.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::borrow::Cow;
22

33
use jiter::{JsonArray, JsonValue};
4-
use num_bigint::BigInt;
54
use pyo3::prelude::*;
65
use pyo3::types::{PyDict, PyString};
76
use speedate::MicrosecondsPrecisionOverflowBehavior;
@@ -371,13 +370,7 @@ impl<'a> Input<'a> for String {
371370
}
372371

373372
fn validate_int(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherInt<'a>>> {
374-
if let Ok(i) = self.parse::<i64>() {
375-
Ok(ValidationMatch::strict(EitherInt::I64(i)))
376-
} else if let Ok(b) = self.parse::<BigInt>() {
377-
Ok(ValidationMatch::exact(EitherInt::BigInt(b.clone())))
378-
} else {
379-
Err(ValError::new(ErrorTypeDefaults::IntParsing, self))
380-
}
373+
str_as_int(self, self).map(ValidationMatch::lax)
381374
}
382375

383376
fn validate_float(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherFloat<'a>>> {

src/input/input_string.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use num_bigint::BigInt;
21
use pyo3::prelude::*;
32
use pyo3::types::{PyDict, PyString};
43

@@ -12,7 +11,7 @@ use crate::validators::decimal::create_decimal;
1211
use super::datetime::{
1312
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, EitherDate, EitherDateTime, EitherTime,
1413
};
15-
use super::shared::{str_as_bool, str_as_float};
14+
use super::shared::{str_as_bool, str_as_float, str_as_int};
1615
use super::{
1716
BorrowInput, EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable,
1817
GenericIterator, GenericMapping, Input, ValidationMatch,
@@ -113,16 +112,7 @@ impl<'a> Input<'a> for StringMapping<'a> {
113112

114113
fn validate_int(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherInt<'a>>> {
115114
match self {
116-
Self::String(s) => {
117-
let str_val = py_string_str(s)?;
118-
if let Ok(i) = str_val.parse::<i64>() {
119-
Ok(ValidationMatch::strict(EitherInt::I64(i)))
120-
} else if let Ok(b) = str_val.parse::<BigInt>() {
121-
Ok(ValidationMatch::exact(EitherInt::BigInt(b.clone())))
122-
} else {
123-
Err(ValError::new(ErrorTypeDefaults::IntParsing, self))
124-
}
125-
},
115+
Self::String(s) => str_as_int(self, py_string_str(s)?).map(ValidationMatch::strict),
126116
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::IntType, self)),
127117
}
128118
}

tests/validators/test_int.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,10 @@ def test_allow_inf_nan_false_json() -> None:
504504
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
505505
v.validate_json('-Infinity')
506506

507+
507508
def test_json_big_int_key():
508509
v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'int'}, 'values_schema': {'type': 'str'}})
509510
big_integer = 1433352099889938534014333520998899385340
510511
assert v.validate_python({big_integer: 'x'}) == {big_integer: 'x'}
511-
512512
assert v.validate_json('{"' + str(big_integer) + '": "x"}') == {big_integer: 'x'}
513-
"""
514-
pydantic_core._pydantic_core.ValidationError: 1 validation error for dict[int,str]
515-
1433352099889938534014333520998899385340.[key]
516-
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='1433352099889938534014333520998899385340', input_type=str]
517-
For further information visit https://errors.pydantic.dev/latest/v/int_parsing
518-
"""
513+
assert v.validate_strings({str(big_integer): 'x'}) == {big_integer: 'x'}

0 commit comments

Comments
 (0)