-
Notifications
You must be signed in to change notification settings - Fork 292
Implement val_temporal_unit for deciding how datetimes and dates timestamps get validated. #1751
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
Open
ollz272
wants to merge
13
commits into
pydantic:main
Choose a base branch
from
ollz272:val_temporal_unit_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8ca19d8
test: add tests for new logic
ollz272 c9f8fac
fix: add validator switch
ollz272 d611124
fix: wip
ollz272 34e3d23
feat: add support for datetimes
ollz272 e1e7cc4
feat: add date
ollz272 987b424
fix: some stuff for time, needs more support in speedate
ollz272 ef3814f
feat: fin
ollz272 7df52a1
fix: lint
ollz272 11ed996
fix: add some tests with the same timestamps but with seconds, millis…
ollz272 7721a53
fix: format
ollz272 e476efb
Merge branch 'main' into val_temporal_unit_2
ollz272 67fcdc3
fix: fix issue with fractional part of mstimestamps
ollz272 e984613
style: lint
ollz272 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use pyo3::pyclass::CompareOp; | |
use pyo3::types::PyTuple; | ||
use pyo3::types::{PyDate, PyDateTime, PyDelta, PyDeltaAccess, PyDict, PyTime, PyTzInfo}; | ||
use pyo3::IntoPyObjectExt; | ||
use speedate::DateConfig; | ||
use speedate::{ | ||
Date, DateTime, DateTimeConfig, Duration, MicrosecondsPrecisionOverflowBehavior, ParseError, Time, TimeConfig, | ||
}; | ||
|
@@ -21,6 +22,7 @@ use super::Input; | |
use crate::errors::ToErrorValue; | ||
use crate::errors::{ErrorType, ValError, ValResult}; | ||
use crate::tools::py_err; | ||
use crate::validators::TemporalUnitMode; | ||
|
||
#[cfg_attr(debug_assertions, derive(Debug))] | ||
pub enum EitherDate<'py> { | ||
|
@@ -324,8 +326,12 @@ impl<'py> EitherDateTime<'py> { | |
} | ||
} | ||
|
||
pub fn bytes_as_date<'py>(input: &(impl Input<'py> + ?Sized), bytes: &[u8]) -> ValResult<EitherDate<'py>> { | ||
match Date::parse_bytes(bytes) { | ||
pub fn bytes_as_date<'py>( | ||
input: &(impl Input<'py> + ?Sized), | ||
bytes: &[u8], | ||
mode: TemporalUnitMode, | ||
) -> ValResult<EitherDate<'py>> { | ||
match Date::parse_bytes_with_config(bytes, &DateConfig::builder().timestamp_unit(mode.into()).build()) { | ||
Ok(date) => Ok(date.into()), | ||
Err(err) => Err(ValError::new( | ||
ErrorType::DateParsing { | ||
|
@@ -364,6 +370,7 @@ pub fn bytes_as_datetime<'py>( | |
input: &(impl Input<'py> + ?Sized), | ||
bytes: &[u8], | ||
microseconds_overflow_behavior: MicrosecondsPrecisionOverflowBehavior, | ||
mode: TemporalUnitMode, | ||
) -> ValResult<EitherDateTime<'py>> { | ||
match DateTime::parse_bytes_with_config( | ||
bytes, | ||
|
@@ -372,7 +379,7 @@ pub fn bytes_as_datetime<'py>( | |
microseconds_precision_overflow_behavior: microseconds_overflow_behavior, | ||
unix_timestamp_offset: Some(0), | ||
}, | ||
..Default::default() | ||
timestamp_unit: mode.into(), | ||
}, | ||
) { | ||
Ok(dt) => Ok(dt.into()), | ||
|
@@ -390,6 +397,7 @@ pub fn int_as_datetime<'py>( | |
input: &(impl Input<'py> + ?Sized), | ||
timestamp: i64, | ||
timestamp_microseconds: u32, | ||
mode: TemporalUnitMode, | ||
) -> ValResult<EitherDateTime<'py>> { | ||
match DateTime::from_timestamp_with_config( | ||
timestamp, | ||
|
@@ -399,7 +407,7 @@ pub fn int_as_datetime<'py>( | |
unix_timestamp_offset: Some(0), | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
timestamp_unit: mode.into(), | ||
}, | ||
) { | ||
Ok(dt) => Ok(dt.into()), | ||
|
@@ -427,12 +435,30 @@ macro_rules! nan_check { | |
}; | ||
} | ||
|
||
pub fn float_as_datetime<'py>(input: &(impl Input<'py> + ?Sized), timestamp: f64) -> ValResult<EitherDateTime<'py>> { | ||
pub fn float_as_datetime<'py>( | ||
input: &(impl Input<'py> + ?Sized), | ||
timestamp: f64, | ||
mode: TemporalUnitMode, | ||
) -> ValResult<EitherDateTime<'py>> { | ||
nan_check!(input, timestamp, DatetimeParsing); | ||
let microseconds = timestamp.fract().abs() * 1_000_000.0; | ||
let microseconds = match mode { | ||
TemporalUnitMode::Seconds => timestamp.fract().abs() * 1_000_000.0, | ||
TemporalUnitMode::Milliseconds => timestamp.fract().abs() * 1_000.0, | ||
TemporalUnitMode::Infer => { | ||
// Use the same watershed from speedate to determine if we treat the float as seconds or milliseconds. | ||
// TODO: should we expose this from speedate? | ||
if timestamp.abs() <= 20_000_000_000.0 { | ||
Comment on lines
+448
to
+450
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially worth adding a utility function in speedate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// treat as seconds | ||
timestamp.fract().abs() * 1_000_000.0 | ||
} else { | ||
// treat as milliseconds | ||
timestamp.fract().abs() * 1_000.0 | ||
} | ||
} | ||
}; | ||
// checking for extra digits in microseconds is unreliable with large floats, | ||
// so we just round to the nearest microsecond | ||
int_as_datetime(input, timestamp.floor() as i64, microseconds.round() as u32) | ||
int_as_datetime(input, timestamp.floor() as i64, microseconds.round() as u32, mode) | ||
} | ||
|
||
pub fn date_as_datetime<'py>(date: &Bound<'py, PyDate>) -> PyResult<EitherDateTime<'py>> { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it makes sense to just pass
&DateTimeConfig
here to simplify things?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i chose not to as i wasn't a fan of using something from the serialization module in the validation logic. Happy to be overridden though!