Skip to content

Commit 306cab7

Browse files
committed
Feedback
1 parent d5434f7 commit 306cab7

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/validators/decimal.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pyo3::exceptions::{PyTypeError, PyValueError};
22
use pyo3::intern;
33
use pyo3::sync::GILOnceCell;
4-
use pyo3::types::{IntoPyDict, PyDict, PyTuple, PyType};
4+
use pyo3::types::{IntoPyDict, PyDict, PyString, PyTuple, PyType};
55
use pyo3::{prelude::*, PyTypeInfo};
66

77
use crate::build_tools::{is_strict, schema_or_config_same};
@@ -28,7 +28,11 @@ pub fn get_decimal_type(py: Python) -> &Bound<'_, PyType> {
2828
.bind(py)
2929
}
3030

31-
fn validate_as_decimal(py: Python, schema: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<Py<PyAny>>> {
31+
fn validate_as_decimal(
32+
py: Python,
33+
schema: &Bound<'_, PyDict>,
34+
key: &Bound<'_, PyString>,
35+
) -> PyResult<Option<Py<PyAny>>> {
3236
match schema.get_item(key)? {
3337
Some(value) => match value.validate_decimal(false, py) {
3438
Ok(v) => Ok(Some(v.into_inner().unbind())),
@@ -77,11 +81,11 @@ impl BuildValidator for DecimalValidator {
7781
allow_inf_nan,
7882
check_digits: decimal_places.is_some() || max_digits.is_some(),
7983
decimal_places,
80-
multiple_of: validate_as_decimal(py, schema, "multiple_of")?,
81-
le: validate_as_decimal(py, schema, "le")?,
82-
lt: validate_as_decimal(py, schema, "lt")?,
83-
ge: validate_as_decimal(py, schema, "ge")?,
84-
gt: validate_as_decimal(py, schema, "gt")?,
84+
multiple_of: validate_as_decimal(py, schema, intern!(py, "multiple_of"))?,
85+
le: validate_as_decimal(py, schema, intern!(py, "le"))?,
86+
lt: validate_as_decimal(py, schema, intern!(py, "lt"))?,
87+
ge: validate_as_decimal(py, schema, intern!(py, "ge"))?,
88+
gt: validate_as_decimal(py, schema, intern!(py, "gt"))?,
8589
max_digits,
8690
}
8791
.into())

src/validators/int.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use num_bigint::BigInt;
22
use pyo3::exceptions::PyValueError;
33
use pyo3::intern;
44
use pyo3::prelude::*;
5-
use pyo3::types::PyDict;
5+
use pyo3::types::{PyDict, PyString};
66
use pyo3::IntoPyObjectExt;
77

88
use crate::build_tools::is_strict;
@@ -11,7 +11,7 @@ use crate::input::{Input, Int};
1111

1212
use super::{BuildValidator, CombinedValidator, DefinitionsBuilder, ValidationState, Validator};
1313

14-
fn validate_as_int(schema: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<Int>> {
14+
fn validate_as_int(schema: &Bound<'_, PyDict>, key: &Bound<'_, PyString>) -> PyResult<Option<Int>> {
1515
match schema.get_item(key)? {
1616
Some(value) => match value.validate_int(false) {
1717
Ok(v) => match v.into_inner().as_int() {
@@ -89,13 +89,14 @@ pub struct ConstrainedIntValidator {
8989

9090
impl ConstrainedIntValidator {
9191
fn build(schema: &Bound<'_, PyDict>, config: Option<&Bound<'_, PyDict>>) -> PyResult<CombinedValidator> {
92+
let py = schema.py();
9293
Ok(Self {
9394
strict: is_strict(schema, config)?,
94-
multiple_of: validate_as_int(schema, "multiple_of")?,
95-
le: validate_as_int(schema, "le")?,
96-
lt: validate_as_int(schema, "lt")?,
97-
ge: validate_as_int(schema, "ge")?,
98-
gt: validate_as_int(schema, "gt")?,
95+
multiple_of: validate_as_int(schema, intern!(py, "multiple_of"))?,
96+
le: validate_as_int(schema, intern!(py, "le"))?,
97+
lt: validate_as_int(schema, intern!(py, "lt"))?,
98+
ge: validate_as_int(schema, intern!(py, "ge"))?,
99+
gt: validate_as_int(schema, intern!(py, "gt"))?,
99100
}
100101
.into())
101102
}

src/validators/timedelta.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use pyo3::exceptions::PyValueError;
2+
use pyo3::intern;
23
use pyo3::prelude::*;
3-
use pyo3::types::{PyDelta, PyDeltaAccess, PyDict};
4+
use pyo3::types::{PyDelta, PyDeltaAccess, PyDict, PyString};
45
use speedate::{Duration, MicrosecondsPrecisionOverflowBehavior};
56

67
use crate::build_tools::is_strict;
@@ -25,7 +26,7 @@ struct TimedeltaConstraints {
2526
gt: Option<Duration>,
2627
}
2728

28-
fn get_constraint(schema: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<Duration>> {
29+
fn get_constraint(schema: &Bound<'_, PyDict>, key: &Bound<'_, PyString>) -> PyResult<Option<Duration>> {
2930
match schema.get_item(key)? {
3031
Some(value) => match value.validate_timedelta(false, MicrosecondsPrecisionOverflowBehavior::default()) {
3132
Ok(v) => Ok(Some(v.into_inner().to_duration()?)),
@@ -45,11 +46,12 @@ impl BuildValidator for TimeDeltaValidator {
4546
config: Option<&Bound<'_, PyDict>>,
4647
_definitions: &mut DefinitionsBuilder<CombinedValidator>,
4748
) -> PyResult<CombinedValidator> {
49+
let py = schema.py();
4850
let constraints = TimedeltaConstraints {
49-
le: get_constraint(schema, "le")?,
50-
lt: get_constraint(schema, "lt")?,
51-
ge: get_constraint(schema, "ge")?,
52-
gt: get_constraint(schema, "gt")?,
51+
le: get_constraint(schema, intern!(py, "le"))?,
52+
lt: get_constraint(schema, intern!(py, "lt"))?,
53+
ge: get_constraint(schema, intern!(py, "ge"))?,
54+
gt: get_constraint(schema, intern!(py, "gt"))?,
5355
};
5456

5557
Ok(Self {

0 commit comments

Comments
 (0)