Skip to content

Commit

Permalink
refactor: Refactored the way of warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hh-space-invader committed Dec 16, 2024
1 parent 88543cb commit 68223bd
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 54 deletions.
1 change: 1 addition & 0 deletions qdrant_client/async_qdrant_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,7 @@ async def upsert(
message="\n Usage of `grpc.PointStruct` is deprecated. Please use `models.PointStruct` instead.\n ",
category=DeprecationWarning,
idx="grpc-input",
stacklevel=4,
)
requires_inference = self._inference_inspector.inspect(points)
if requires_inference and (not self.cloud_inference):
Expand Down
8 changes: 4 additions & 4 deletions qdrant_client/async_qdrant_fastembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# ****** WARNING: THIS FILE IS AUTOGENERATED ******

import uuid
import warnings
from itertools import tee
from typing import Any, Iterable, Optional, Sequence, Union, get_args
from copy import deepcopy
Expand All @@ -29,6 +28,7 @@
from qdrant_client.http import models
from qdrant_client.hybrid.fusion import reciprocal_rank_fusion
from qdrant_client import grpc
from qdrant_client.common.client_warnings import show_warning

try:
from fastembed import (
Expand Down Expand Up @@ -151,9 +151,9 @@ def set_model(
None
"""
if max_length is not None:
warnings.warn(
"max_length parameter is deprecated and will be removed in the future. It's not used by fastembed models.",
DeprecationWarning,
show_warning(
message="max_length parameter is deprecated and will be removed in the future. It's not used by fastembed models.",
category=DeprecationWarning,
stacklevel=2,
)
self._get_or_init_model(
Expand Down
30 changes: 23 additions & 7 deletions qdrant_client/async_qdrant_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import logging
import math
import platform
import warnings
from multiprocessing import get_all_start_methods
from typing import (
Any,
Expand All @@ -31,6 +30,7 @@
import numpy as np
from grpc import Compression
from urllib3.util import Url, parse_url
from qdrant_client.common.client_warnings import show_warning
from qdrant_client import grpc as grpc
from qdrant_client._pydantic_compat import construct
from qdrant_client.auth import BearerAuth
Expand Down Expand Up @@ -115,7 +115,11 @@ def __init__(
self._rest_headers = kwargs.pop("metadata", {})
if api_key is not None:
if self._scheme == "http":
warnings.warn("Api key is used with an insecure connection.")
show_warning(
message="Api key is used with an insecure connection.",
category=RuntimeWarning,
stacklevel=2,
)
self._rest_headers["api-key"] = api_key
self._grpc_headers.append(("api-key", api_key))
client_version = importlib.metadata.version("qdrant-client")
Expand Down Expand Up @@ -145,7 +149,11 @@ def __init__(
self._timeout = self.DEFAULT_GRPC_TIMEOUT
if self._auth_token_provider is not None:
if self._scheme == "http":
warnings.warn("Auth token provider is used with an insecure connection.")
show_warning(
message="Auth token provider is used with an insecure connection.",
category=RuntimeWarning,
stacklevel=2,
)
bearer_auth = BearerAuth(self._auth_token_provider)
self._rest_args["auth"] = bearer_auth
self.openapi_client: AsyncApis[AsyncApiClient] = AsyncApis(
Expand Down Expand Up @@ -352,8 +360,10 @@ async def search(
**kwargs: Any,
) -> list[types.ScoredPoint]:
if not append_payload:
logging.warning(
"Usage of `append_payload` is deprecated. Please consider using `with_payload` instead"
show_warning(
message="Usage of `append_payload` is deprecated. Please consider using `with_payload` instead",
category=DeprecationWarning,
stacklevel=2,
)
with_payload = append_payload
if isinstance(query_vector, np.ndarray):
Expand Down Expand Up @@ -2414,7 +2424,9 @@ async def create_collection(
**kwargs: Any,
) -> bool:
if init_from is not None:
logging.warning("init_from is deprecated")
show_warning(
message="init_from is deprecated", category=DeprecationWarning, stacklevel=2
)
if self._prefer_grpc:
if isinstance(vectors_config, (models.VectorParams, dict)):
vectors_config = RestToGrpc.convert_vectors_config(vectors_config)
Expand Down Expand Up @@ -2670,7 +2682,11 @@ async def create_payload_index(
**kwargs: Any,
) -> types.UpdateResult:
if field_type is not None:
warnings.warn("field_type is deprecated, use field_schema instead", DeprecationWarning)
show_warning(
message="field_type is deprecated, use field_schema instead",
category=DeprecationWarning,
stacklevel=2,
)
field_schema = field_type
if self._prefer_grpc:
field_index_params = None
Expand Down
11 changes: 7 additions & 4 deletions qdrant_client/common/client_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
SEEN_MESSAGES = set()


def show_warning(message: str, category: type[Warning] = UserWarning) -> None:
warnings.warn(message, category, stacklevel=4)
def show_warning(message: str, category: type[Warning] = UserWarning, stacklevel: int = 1) -> None:
warnings.warn(message, category, stacklevel=stacklevel)


def show_warning_once(
message: str, category: type[Warning] = UserWarning, idx: Optional[str] = None
message: str,
category: type[Warning] = UserWarning,
idx: Optional[str] = None,
stacklevel: int = 1,
) -> None:
"""
Show a warning of the specified category only once per program run.
Expand All @@ -18,4 +21,4 @@ def show_warning_once(

if key not in SEEN_MESSAGES:
SEEN_MESSAGES.add(key)
show_warning(message, category)
show_warning(message, category, stacklevel)
24 changes: 16 additions & 8 deletions qdrant_client/local/async_qdrant_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import importlib.metadata
import itertools
import json
import logging
import os
import shutil
from copy import deepcopy
Expand All @@ -21,7 +20,7 @@
from uuid import uuid4
import numpy as np
import portalocker
from qdrant_client.common.client_warnings import show_warning_once
from qdrant_client.common.client_warnings import show_warning, show_warning_once
from qdrant_client._pydantic_compat import to_dict
from qdrant_client.async_client_base import AsyncQdrantBase
from qdrant_client.conversions import common_types as types
Expand Down Expand Up @@ -77,8 +76,10 @@ async def close(self, **kwargs: Any) -> None:
if collection is not None:
collection.close()
else:
logging.warning(
f"Collection appears to be None before closing. The existing collections are: {list(self.collections.keys())}"
show_warning(
message=f"Collection appears to be None before closing. The existing collections are: {list(self.collections.keys())}",
category=UserWarning,
stacklevel=1,
)
try:
if self._flock_file is not None and (not self._flock_file.closed):
Expand Down Expand Up @@ -112,6 +113,7 @@ def _load(self) -> None:
f"Local mode is not recommended for collections with more than {self.LARGE_DATA_THRESHOLD:,} points. Collection <{collection_name}> contains {len(collection.ids)} points. Consider using Qdrant in Docker or Qdrant Cloud for better performance with large datasets.",
category=UserWarning,
idx="large-local-collection",
stacklevel=4,
)
self.aliases = meta["aliases"]
lock_file_path = os.path.join(self.location, ".lock")
Expand Down Expand Up @@ -1044,16 +1046,22 @@ async def create_payload_index(
field_type: Optional[types.PayloadSchemaType] = None,
**kwargs: Any,
) -> types.UpdateResult:
logging.warning(
"Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes."
show_warning_once(
message="Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes.",
category=UserWarning,
idx="server-payload-indexes",
stacklevel=1,
)
return self._default_update_result()

async def delete_payload_index(
self, collection_name: str, field_name: str, **kwargs: Any
) -> types.UpdateResult:
logging.warning(
"Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes."
show_warning_once(
message="Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes.",
category=UserWarning,
idx="server-payload-indexes",
stacklevel=1,
)
return self._default_update_result()

Expand Down
1 change: 1 addition & 0 deletions qdrant_client/local/local_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,7 @@ def upsert(self, points: Union[Sequence[models.PointStruct], models.Batch]) -> N
"Consider using Qdrant in Docker or Qdrant Cloud for better performance with large datasets.",
category=UserWarning,
idx="large-local-collection",
stacklevel=4,
)

def _update_named_vectors(
Expand Down
25 changes: 17 additions & 8 deletions qdrant_client/local/qdrant_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import numpy as np
import portalocker

from qdrant_client.common.client_warnings import show_warning_once
from qdrant_client.common.client_warnings import show_warning, show_warning_once
from qdrant_client._pydantic_compat import to_dict
from qdrant_client.client_base import QdrantBase
from qdrant_client.conversions import common_types as types
Expand Down Expand Up @@ -77,9 +77,11 @@ def close(self, **kwargs: Any) -> None:
if collection is not None:
collection.close()
else:
logging.warning(
f"Collection appears to be None before closing. The existing collections are: "
f"{list(self.collections.keys())}"
show_warning(
message=f"Collection appears to be None before closing. The existing collections are: "
f"{list(self.collections.keys())}",
category=UserWarning,
stacklevel=1,
)

try:
Expand Down Expand Up @@ -119,6 +121,7 @@ def _load(self) -> None:
"with large datasets.",
category=UserWarning,
idx="large-local-collection",
stacklevel=4,
)
self.aliases = meta["aliases"]

Expand Down Expand Up @@ -1133,16 +1136,22 @@ def create_payload_index(
field_type: Optional[types.PayloadSchemaType] = None,
**kwargs: Any,
) -> types.UpdateResult:
logging.warning(
"Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes."
show_warning_once(
message="Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes.",
category=UserWarning,
idx="server-payload-indexes",
stacklevel=1,
)
return self._default_update_result()

def delete_payload_index(
self, collection_name: str, field_name: str, **kwargs: Any
) -> types.UpdateResult:
logging.warning(
"Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes."
show_warning_once(
message="Payload indexes have no effect in the local Qdrant. Please use server Qdrant if you need payload indexes.",
category=UserWarning,
idx="server-payload-indexes",
stacklevel=1,
)
return self._default_update_result()

Expand Down
1 change: 1 addition & 0 deletions qdrant_client/qdrant_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,7 @@ def upsert(
""",
category=DeprecationWarning,
idx="grpc-input",
stacklevel=4,
)

requires_inference = self._inference_inspector.inspect(points)
Expand Down
7 changes: 4 additions & 3 deletions qdrant_client/qdrant_fastembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qdrant_client.http import models
from qdrant_client.hybrid.fusion import reciprocal_rank_fusion
from qdrant_client import grpc
from qdrant_client.common.client_warnings import show_warning

try:
from fastembed import (
Expand Down Expand Up @@ -153,10 +154,10 @@ def set_model(
"""

if max_length is not None:
warnings.warn(
"max_length parameter is deprecated and will be removed in the future. "
show_warning(
message="max_length parameter is deprecated and will be removed in the future. "
"It's not used by fastembed models.",
DeprecationWarning,
category=DeprecationWarning,
stacklevel=2,
)

Expand Down
Loading

0 comments on commit 68223bd

Please sign in to comment.