diff --git a/src/input/mod.rs b/src/input/mod.rs index 779454b3c..19be9fad1 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -13,7 +13,7 @@ mod shared; pub use datetime::TzInfo; pub(crate) use datetime::{ duration_as_pytimedelta, pydate_as_date, pydatetime_as_datetime, pytime_as_time, EitherDate, EitherDateTime, - EitherTime, EitherTimedelta, + EitherTimedelta, }; pub(crate) use input_abstract::{ Arguments, BorrowInput, ConsumeIterator, Input, InputType, KeywordArgs, PositionalArgs, ValidatedDict, diff --git a/src/validators/time.rs b/src/validators/time.rs index eda4935a3..db98846b1 100644 --- a/src/validators/time.rs +++ b/src/validators/time.rs @@ -1,14 +1,14 @@ +use pyo3::exceptions::PyValueError; use pyo3::intern; use pyo3::prelude::*; use pyo3::types::{PyDict, PyString}; use pyo3::IntoPyObjectExt; -use speedate::Time; +use speedate::{MicrosecondsPrecisionOverflowBehavior, Time}; use crate::build_tools::is_strict; use crate::errors::{ErrorType, ValError, ValResult}; -use crate::input::{EitherTime, Input}; -use crate::tools::SchemaDict; +use crate::input::Input; use super::datetime::extract_microseconds_precision; use super::datetime::TZConstraint; @@ -18,7 +18,7 @@ use super::{BuildValidator, CombinedValidator, DefinitionsBuilder, ValidationSta pub struct TimeValidator { strict: bool, constraints: Option, - microseconds_precision: speedate::MicrosecondsPrecisionOverflowBehavior, + microseconds_precision: MicrosecondsPrecisionOverflowBehavior, } impl BuildValidator for TimeValidator { @@ -86,9 +86,14 @@ impl Validator for TimeValidator { } } -fn convert_pytime(schema: &Bound<'_, PyDict>, field: &Bound<'_, PyString>) -> PyResult> { - match schema.get_as(field)? { - Some(date) => Ok(Some(EitherTime::Py(date).as_raw()?)), +fn convert_pytime(schema: &Bound<'_, PyDict>, key: &Bound<'_, PyString>) -> PyResult> { + match schema.get_item(key)? { + Some(value) => match value.validate_time(false, MicrosecondsPrecisionOverflowBehavior::default()) { + Ok(v) => Ok(Some(v.into_inner().as_raw()?)), + Err(_) => Err(PyValueError::new_err(format!( + "'{key}' must be coercible to a time instance", + ))), + }, None => Ok(None), } } diff --git a/tests/validators/test_time.py b/tests/validators/test_time.py index 069e359ad..c5f5e3c39 100644 --- a/tests/validators/test_time.py +++ b/tests/validators/test_time.py @@ -10,6 +10,15 @@ from ..conftest import Err, PyAndJson +@pytest.mark.parametrize( + 'constraint', + ['le', 'lt', 'ge', 'gt'], +) +def test_constraints_schema_validation_error(constraint: str) -> None: + with pytest.raises(SchemaError, match=f"'{constraint}' must be coercible to a time instance"): + SchemaValidator(core_schema.time_schema(**{constraint: 'bad_value'})) + + @pytest.mark.parametrize( 'input_value,expected', [