1
+ use pyo3:: exceptions:: PyValueError ;
2
+ use pyo3:: intern;
1
3
use 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 } ;
4
6
5
7
use crate :: build_tools:: is_strict;
6
8
use crate :: errors:: { ErrorType , ValError , ValResult } ;
7
- use crate :: input:: { duration_as_pytimedelta, EitherTimedelta , Input } ;
9
+ use crate :: input:: { duration_as_pytimedelta, Input } ;
8
10
9
11
use super :: datetime:: extract_microseconds_precision;
10
12
use super :: { BuildValidator , CombinedValidator , DefinitionsBuilder , ValidationState , Validator } ;
@@ -24,12 +26,14 @@ struct TimedeltaConstraints {
24
26
gt : Option < Duration > ,
25
27
}
26
28
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 > > {
28
30
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
+ } ,
33
37
None => Ok ( None ) ,
34
38
}
35
39
}
@@ -42,11 +46,12 @@ impl BuildValidator for TimeDeltaValidator {
42
46
config : Option < & Bound < ' _ , PyDict > > ,
43
47
_definitions : & mut DefinitionsBuilder < CombinedValidator > ,
44
48
) -> PyResult < CombinedValidator > {
49
+ let py = schema. py ( ) ;
45
50
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" ) ) ?,
50
55
} ;
51
56
52
57
Ok ( Self {
0 commit comments