Skip to content

Commit

Permalink
fix: fix date object grpc conversion (#580)
Browse files Browse the repository at this point in the history
* fix: fix date object grpc conversion

* fix: fix date to datetime conversion in filters in local mode

* fix: fix date isinstance check
  • Loading branch information
joein authored Apr 5, 2024
1 parent 9e56ed6 commit bd6e755
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion qdrant_client/conversions/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,9 @@ def convert_range(cls, model: rest.Range) -> grpc.Range:
)

@classmethod
def convert_datetime(cls, model: datetime) -> Timestamp:
def convert_datetime(cls, model: Union[datetime, date]) -> Timestamp:
if isinstance(model, date) and not isinstance(model, datetime):
model = datetime.combine(model, datetime.min.time())
ts = Timestamp()
ts.FromDatetime(model)
return ts
Expand Down
2 changes: 1 addition & 1 deletion qdrant_client/local/payload_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def check_range(condition: models.Range, value: Any) -> bool:

def check_datetime_range(condition: models.DatetimeRange, value: Any) -> bool:
def make_condition_tz_aware(dt: Optional[Union[datetime, date]]) -> Optional[datetime]:
if isinstance(dt, date):
if isinstance(dt, date) and not isinstance(dt, datetime):
dt = datetime.combine(dt, datetime.min.time())

if dt is None or dt.tzinfo is not None:
Expand Down
9 changes: 7 additions & 2 deletions tests/conversions/test_validate_conversions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import inspect
import logging
import re
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from inspect import getmembers
from typing import Union

import pytest
from google.protobuf.json_format import MessageToDict
Expand Down Expand Up @@ -264,14 +265,18 @@ def test_init_from_conversion():
datetime(2021, 1, 1, 0, 0, 0),
datetime.utcnow(),
datetime.now(),
date.today(),
],
)
def test_datetime_to_timestamp_conversions(dt: datetime):
def test_datetime_to_timestamp_conversions(dt: Union[datetime, date]):
from qdrant_client.conversions.conversion import GrpcToRest, RestToGrpc

rest_to_grpc = RestToGrpc.convert_datetime(dt)
grpc_to_rest = GrpcToRest.convert_timestamp(rest_to_grpc)

if isinstance(dt, date) and not isinstance(dt, datetime):
dt = datetime.combine(dt, datetime.min.time())

assert (
dt.utctimetuple() == grpc_to_rest.utctimetuple()
), f"Failed for {dt}, should be equal to {grpc_to_rest}"

0 comments on commit bd6e755

Please sign in to comment.