1+ use pyo3:: exceptions:: PyValueError ;
2+ use pyo3:: intern;
13use pyo3:: prelude:: * ;
2- use pyo3:: types:: { PyDelta , PyDeltaAccess , PyDict } ;
3- use speedate:: Duration ;
4+ use pyo3:: types:: { PyDelta , PyDeltaAccess , PyDict , PyString } ;
5+ use speedate:: { Duration , MicrosecondsPrecisionOverflowBehavior } ;
46
57use crate :: build_tools:: is_strict;
68use crate :: errors:: { ErrorType , ValError , ValResult } ;
7- use crate :: input:: { duration_as_pytimedelta, EitherTimedelta , Input } ;
9+ use crate :: input:: { duration_as_pytimedelta, Input } ;
810
911use super :: datetime:: extract_microseconds_precision;
1012use super :: { BuildValidator , CombinedValidator , DefinitionsBuilder , ValidationState , Validator } ;
@@ -24,12 +26,14 @@ struct TimedeltaConstraints {
2426 gt : Option < Duration > ,
2527}
2628
27- fn get_constraint ( schema : & Bound < ' _ , PyDict > , key : & str ) -> PyResult < Option < Duration > > {
29+ fn get_constraint ( schema : & Bound < ' _ , PyDict > , key : & Bound < ' _ , PyString > ) -> PyResult < Option < Duration > > {
2830 match schema. get_item ( key) ? {
29- Some ( value) => {
30- let either_timedelta = EitherTimedelta :: try_from ( & value) ?;
31- Ok ( Some ( either_timedelta. to_duration ( ) ?) )
32- }
31+ Some ( value) => match value. validate_timedelta ( false , MicrosecondsPrecisionOverflowBehavior :: default ( ) ) {
32+ Ok ( v) => Ok ( Some ( v. into_inner ( ) . to_duration ( ) ?) ) ,
33+ Err ( _) => Err ( PyValueError :: new_err ( format ! (
34+ "'{key}' must be coercible to a timedelta instance"
35+ ) ) ) ,
36+ } ,
3337 None => Ok ( None ) ,
3438 }
3539}
@@ -42,11 +46,12 @@ impl BuildValidator for TimeDeltaValidator {
4246 config : Option < & Bound < ' _ , PyDict > > ,
4347 _definitions : & mut DefinitionsBuilder < CombinedValidator > ,
4448 ) -> PyResult < CombinedValidator > {
49+ let py = schema. py ( ) ;
4550 let constraints = TimedeltaConstraints {
46- le : get_constraint ( schema, "le" ) ?,
47- lt : get_constraint ( schema, "lt" ) ?,
48- ge : get_constraint ( schema, "ge" ) ?,
49- 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" ) ) ?,
5055 } ;
5156
5257 Ok ( Self {
0 commit comments