Skip to content

Coerce 'time' schema constraints #1720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions src/validators/time.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +18,7 @@ use super::{BuildValidator, CombinedValidator, DefinitionsBuilder, ValidationSta
pub struct TimeValidator {
strict: bool,
constraints: Option<TimeConstraints>,
microseconds_precision: speedate::MicrosecondsPrecisionOverflowBehavior,
microseconds_precision: MicrosecondsPrecisionOverflowBehavior,
}

impl BuildValidator for TimeValidator {
Expand Down Expand Up @@ -86,9 +86,14 @@ impl Validator for TimeValidator {
}
}

fn convert_pytime(schema: &Bound<'_, PyDict>, field: &Bound<'_, PyString>) -> PyResult<Option<Time>> {
match schema.get_as(field)? {
Some(date) => Ok(Some(EitherTime::Py(date).as_raw()?)),
fn convert_pytime(schema: &Bound<'_, PyDict>, key: &Bound<'_, PyString>) -> PyResult<Option<Time>> {
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),
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/validators/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand Down
Loading