-
Notifications
You must be signed in to change notification settings - Fork 290
fix: treat a naive datetime as UTC in order_by #1352
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
Closed
shashvat-singham
wants to merge
3
commits into
qdrant:dev
from
shashvat-singham:fix/naive-datetime-is-utc
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from datetime import datetime, timedelta, timezone, tzinfo | ||
|
|
||
| from qdrant_client.local.order_by import to_order_value | ||
|
|
||
|
|
||
| def test_naive_datetime_is_normalized_to_utc(monkeypatch) -> None: | ||
| """Assert the normalization itself, not the resulting number. | ||
|
|
||
| On a UTC host `timestamp()` happens to agree with the UTC interpretation, so a | ||
| value-only assertion passes even without the fix. Spying on what reaches | ||
| `datetime_to_microseconds` makes this fail on any host timezone. | ||
| """ | ||
| import qdrant_client.local.order_by as order_by_module | ||
|
|
||
| seen: dict[str, object] = {} | ||
| real = order_by_module.datetime_to_microseconds | ||
|
|
||
| def spy(dt: datetime) -> int: | ||
| seen["tzinfo"] = dt.tzinfo | ||
| seen["utcoffset"] = dt.utcoffset() | ||
| return real(dt) | ||
|
|
||
| monkeypatch.setattr(order_by_module, "datetime_to_microseconds", spy) | ||
| order_by_module.to_order_value(datetime(2024, 6, 15, 12, 30, 45)) | ||
|
|
||
| assert seen["tzinfo"] is timezone.utc | ||
| assert seen["utcoffset"] == timedelta(0) | ||
|
|
||
|
|
||
| def test_naive_datetime_is_utc_like_a_naive_string() -> None: | ||
| """A naive datetime means UTC, matching how a naive datetime *string* is parsed. | ||
|
|
||
| Reading it as local time made the order value depend on the client machine's | ||
| timezone, so the same wall clock sorted differently on different machines. | ||
| """ | ||
| from_string = to_order_value("2024-06-15 12:30:45") | ||
| from_naive = to_order_value(datetime(2024, 6, 15, 12, 30, 45)) | ||
| from_aware = to_order_value(datetime(2024, 6, 15, 12, 30, 45, tzinfo=timezone.utc)) | ||
|
|
||
| assert from_string == from_naive == from_aware | ||
|
|
||
|
|
||
| def test_aware_datetime_offset_is_respected() -> None: | ||
| ist = timezone(timedelta(hours=5, minutes=30)) | ||
| assert to_order_value(datetime(2024, 6, 15, 18, 0, 45, tzinfo=ist)) == to_order_value( | ||
| datetime(2024, 6, 15, 12, 30, 45, tzinfo=timezone.utc) | ||
| ) | ||
|
|
||
|
|
||
| def test_non_datetime_values_are_unchanged() -> None: | ||
| assert to_order_value(None) is None | ||
| assert to_order_value(42) == 42 | ||
| assert to_order_value(1.5) == 1.5 | ||
| assert to_order_value("not a date") is None | ||
|
|
||
|
|
||
| def test_tzinfo_with_none_utcoffset_is_treated_as_naive() -> None: | ||
| """A tzinfo whose utcoffset() returns None is naive by Python's definition. | ||
|
|
||
| Checking only `tzinfo is None` let such a value through to timestamp(), which | ||
| raised TypeError instead of ordering it. | ||
| """ | ||
|
|
||
| class NoOffset(tzinfo): | ||
| def utcoffset(self, dt): | ||
| return None | ||
|
|
||
| def dst(self, dt): | ||
| return None | ||
|
|
||
| value = datetime(2024, 6, 15, 12, 30, 45, tzinfo=NoOffset()) | ||
| assert value.tzinfo is not None and value.utcoffset() is None # naive per datetime docs | ||
|
|
||
| assert to_order_value(value) == to_order_value( | ||
| datetime(2024, 6, 15, 12, 30, 45, tzinfo=timezone.utc) | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.