Skip to content

Commit e9c7331

Browse files
committed
Revert change to strict_int
1 parent cca08c4 commit e9c7331

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/input/input_abstract.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
124124
self.strict_int()
125125
}
126126

127+
/// Extract an EitherInt from the input, only allowing exact
128+
/// matches for an Int (no subclasses)
129+
fn exact_int(&'a self) -> ValResult<EitherInt<'a>> {
130+
self.strict_int()
131+
}
132+
133+
/// Extract a String from the input, only allowing exact
134+
/// matches for a String (no subclasses)
135+
fn exact_str(&'a self) -> ValResult<EitherString<'a>> {
136+
self.strict_str()
137+
}
138+
127139
fn validate_float(&self, strict: bool, ultra_strict: bool) -> ValResult<f64> {
128140
if ultra_strict {
129141
self.ultra_strict_float()

src/input/input_python.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ impl<'a> Input<'a> for PyAny {
195195
}
196196
}
197197

198+
fn exact_int(&'a self) -> ValResult<EitherInt<'a>> {
199+
if PyInt::is_exact_type_of(self) {
200+
Ok(EitherInt::Py(self))
201+
} else {
202+
Err(ValError::new(ErrorType::IntType, self))
203+
}
204+
}
205+
198206
fn lax_str(&'a self) -> ValResult<EitherString<'a>> {
199207
if let Ok(py_str) = <PyString as PyTryFrom>::try_from_exact(self) {
200208
Ok(py_str.into())
@@ -270,6 +278,13 @@ impl<'a> Input<'a> for PyAny {
270278
fn strict_int(&'a self) -> ValResult<EitherInt<'a>> {
271279
if PyInt::is_exact_type_of(self) {
272280
Ok(EitherInt::Py(self))
281+
} else if PyInt::is_type_of(self) {
282+
// bools are a subclass of int, so check for bool type in this specific case
283+
if PyBool::is_exact_type_of(self) {
284+
Err(ValError::new(ErrorType::IntType, self))
285+
} else {
286+
Ok(EitherInt::Py(self))
287+
}
273288
} else {
274289
Err(ValError::new(ErrorType::IntType, self))
275290
}

src/validators/literal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ impl<T: Clone + Debug> LiteralLookup<T> {
3737
for (k, v) in expected {
3838
let id = values.len();
3939
values.push(v);
40-
if let Ok(either_int) = k.strict_int() {
40+
if let Ok(either_int) = k.exact_int() {
4141
let int = either_int
4242
.into_i64(py)
4343
.map_err(|_| py_schema_error_type!("error extracting int {:?}", k))?;
4444
expected_int.insert(int, id);
45-
} else if let Ok(either_str) = k.strict_str() {
45+
} else if let Ok(either_str) = k.exact_str() {
4646
let str = either_str
4747
.as_cow()
4848
.map_err(|_| py_schema_error_type!("error extracting str {:?}", k))?;
@@ -76,7 +76,7 @@ impl<T: Clone + Debug> LiteralLookup<T> {
7676
) -> ValResult<'data, Option<(&'data I, &T)>> {
7777
// dbg!(input.to_object(py).as_ref(py).repr().unwrap());
7878
if let Some(expected_ints) = &self.expected_int {
79-
if let Ok(either_int) = input.strict_int() {
79+
if let Ok(either_int) = input.exact_int() {
8080
let int = either_int.into_i64(py)?;
8181
if let Some(id) = expected_ints.get(&int) {
8282
return Ok(Some((input, &self.values[*id])));
@@ -85,7 +85,7 @@ impl<T: Clone + Debug> LiteralLookup<T> {
8585
}
8686
if let Some(expected_strings) = &self.expected_str {
8787
// dbg!(expected_strings);
88-
if let Ok(either_str) = input.strict_str() {
88+
if let Ok(either_str) = input.exact_str() {
8989
let cow = either_str.as_cow()?;
9090
if let Some(id) = expected_strings.get(cow.as_ref()) {
9191
return Ok(Some((input, &self.values[*id])));

0 commit comments

Comments
 (0)