Skip to content
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

Add grpc unit test run, expand testing of VectorFactory #326

Merged
merged 7 commits into from
Mar 28, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/testing-unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
name: Unit tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- 3.8
Expand All @@ -28,8 +29,11 @@ jobs:
with:
include_grpc: '${{ matrix.use_grpc }}'
include_types: true
- name: Run unit tests
- name: Run unit tests (REST)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these do cover the REST surface area, but also a lot of the "other" stuff like common utils, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. They're really meant to be "everything that doesn't require GRPC dependencies" unit tests. But that doesn't roll off the tongue 🤣

run: poetry run pytest --cov=pinecone --timeout=120 tests/unit
- name: Run unit tests (GRPC)
if: ${{ matrix.use_grpc == true }}
run: poetry run pytest --cov=pinecone/grpc --timeout=120 tests/unit_grpc
- name: mypy check
env:
INCLUDE_GRPC: '${{ matrix.use_grpc }}'
Expand Down
2 changes: 1 addition & 1 deletion pinecone/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .index import *
from .vector_factory import (
from .errors import (
VectorDictionaryMissingKeysError,
VectorDictionaryExcessKeysError,
VectorTupleLengthError,
Expand Down
37 changes: 37 additions & 0 deletions pinecone/data/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from ..utils.constants import REQUIRED_VECTOR_FIELDS, OPTIONAL_VECTOR_FIELDS

class VectorDictionaryMissingKeysError(ValueError):
def __init__(self, item):
message = f"Vector dictionary is missing required fields: {list(REQUIRED_VECTOR_FIELDS - set(item.keys()))}"
super().__init__(message)

class VectorDictionaryExcessKeysError(ValueError):
def __init__(self, item):
invalid_keys = list(set(item.keys()) - (REQUIRED_VECTOR_FIELDS | OPTIONAL_VECTOR_FIELDS))
message = f"Found excess keys in the vector dictionary: {invalid_keys}. The allowed keys are: {list(REQUIRED_VECTOR_FIELDS | OPTIONAL_VECTOR_FIELDS)}"
super().__init__(message)

class VectorTupleLengthError(ValueError):
def __init__(self, item):
message = f"Found a tuple of length {len(item)} which is not supported. Vectors can be represented as tuples either the form (id, values, metadata) or (id, values). To pass sparse values please use either dicts or Vector objects as inputs."
super().__init__(message)

class SparseValuesTypeError(ValueError, TypeError):
def __init__(self):
message = "Found unexpected data in column `sparse_values`. Expected format is `'sparse_values': {'indices': List[int], 'values': List[float]}`."
super().__init__(message)

class SparseValuesMissingKeysError(ValueError):
def __init__(self, sparse_values_dict):
message = f"Missing required keys in data in column `sparse_values`. Expected format is `'sparse_values': {{'indices': List[int], 'values': List[float]}}`. Found keys {list(sparse_values_dict.keys())}"
super().__init__(message)

class SparseValuesDictionaryExpectedError(ValueError, TypeError):
def __init__(self, sparse_values_dict):
message = f"Column `sparse_values` is expected to be a dictionary, found {type(sparse_values_dict)}"
super().__init__(message)

class MetadataDictionaryExpectedError(ValueError, TypeError):
def __init__(self, item):
message = f"Column `metadata` is expected to be a dictionary, found {type(item['metadata'])}"
super().__init__(message)
54 changes: 54 additions & 0 deletions pinecone/data/sparse_vector_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numbers

from collections.abc import Mapping
from typing import Union, Dict

from ..utils import convert_to_list

from .errors import (
SparseValuesTypeError,
SparseValuesMissingKeysError,
SparseValuesDictionaryExpectedError
)

from pinecone.core.client.models import (
SparseValues
)

class SparseValuesFactory:
@staticmethod
def build(input: Union[Dict, SparseValues]) -> SparseValues:
if input is None:
return input
if isinstance(input, SparseValues):
return input
if not isinstance(input, Mapping):
raise SparseValuesDictionaryExpectedError(input)
if not {"indices", "values"}.issubset(input):
raise SparseValuesMissingKeysError(input)

indices = SparseValuesFactory._convert_to_list(input.get("indices"), int)
values = SparseValuesFactory._convert_to_list(input.get("values"), float)

if len(indices) != len(values):
raise ValueError("Sparse values indices and values must have the same length")

try:
return SparseValues(indices=indices, values=values)
except TypeError as e:
raise SparseValuesTypeError() from e

@staticmethod
def _convert_to_list(input, expected_type):
try:
converted = convert_to_list(input)
except TypeError as e:
raise SparseValuesTypeError() from e

SparseValuesFactory._validate_list_items_type(converted, expected_type)
return converted

@staticmethod
def _validate_list_items_type(input, expected_type):
if len(input) > 0 and not isinstance(input[0], expected_type):
raise SparseValuesTypeError()
63 changes: 11 additions & 52 deletions pinecone/data/vector_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,19 @@

from ..utils import fix_tuple_length, convert_to_list
from ..utils.constants import REQUIRED_VECTOR_FIELDS, OPTIONAL_VECTOR_FIELDS
from .sparse_vector_factory import SparseValuesFactory

from pinecone.core.client.models import (
Vector,
SparseValues
)

class VectorDictionaryMissingKeysError(ValueError):
def __init__(self, item):
message = f"Vector dictionary is missing required fields: {list(REQUIRED_VECTOR_FIELDS - set(item.keys()))}"
super().__init__(message)

class VectorDictionaryExcessKeysError(ValueError):
def __init__(self, item):
invalid_keys = list(set(item.keys()) - (REQUIRED_VECTOR_FIELDS | OPTIONAL_VECTOR_FIELDS))
message = f"Found excess keys in the vector dictionary: {invalid_keys}. The allowed keys are: {list(REQUIRED_VECTOR_FIELDS | OPTIONAL_VECTOR_FIELDS)}"
super().__init__(message)

class VectorTupleLengthError(ValueError):
def __init__(self, item):
message = f"Found a tuple of length {len(item)} which is not supported. Vectors can be represented as tuples either the form (id, values, metadata) or (id, values). To pass sparse values please use either dicts or Vector objects as inputs."
super().__init__(message)

class SparseValuesTypeError(ValueError, TypeError):
def __init__(self):
message = "Found unexpected data in column `sparse_values`. Expected format is `'sparse_values': {'indices': List[int], 'values': List[float]}`."
super().__init__(message)

class SparseValuesMissingKeysError(ValueError):
def __init__(self, sparse_values_dict):
message = f"Missing required keys in data in column `sparse_values`. Expected format is `'sparse_values': {{'indices': List[int], 'values': List[float]}}`. Found keys {list(sparse_values_dict.keys())}"
super().__init__(message)

class SparseValuesDictionaryExpectedError(ValueError, TypeError):
def __init__(self, sparse_values_dict):
message = f"Column `sparse_values` is expected to be a dictionary, found {type(sparse_values_dict)}"
super().__init__(message)

class MetadataDictionaryExpectedError(ValueError, TypeError):
def __init__(self, item):
message = f"Column `metadata` is expected to be a dictionary, found {type(item['metadata'])}"
super().__init__(message)
from .errors import (
VectorDictionaryMissingKeysError,
VectorDictionaryExcessKeysError,
VectorTupleLengthError,
MetadataDictionaryExpectedError,
)

class VectorFactory:
@staticmethod
Expand Down Expand Up @@ -84,8 +56,10 @@ def _dict_to_vector(item, check_type: bool) -> Vector:
item["values"] = convert_to_list(values)

sparse_values = item.get("sparse_values")
if sparse_values and not isinstance(sparse_values, SparseValues):
item["sparse_values"] = VectorFactory._dict_to_sparse_values(sparse_values, check_type)
if sparse_values is None:
item.pop("sparse_values", None)
else:
item["sparse_values"] = SparseValuesFactory.build(sparse_values)

metadata = item.get("metadata")
if metadata and not isinstance(metadata, Mapping):
Expand All @@ -97,18 +71,3 @@ def _dict_to_vector(item, check_type: bool) -> Vector:
if not isinstance(item["values"], Iterable) or not isinstance(item["values"].__iter__().__next__(), numbers.Real):
raise TypeError(f"Column `values` is expected to be a list of floats")
raise e

@staticmethod
def _dict_to_sparse_values(sparse_values_dict: Dict, check_type: bool) -> SparseValues:
if not isinstance(sparse_values_dict, Mapping):
raise SparseValuesDictionaryExpectedError(sparse_values_dict)
if not {"indices", "values"}.issubset(sparse_values_dict):
raise SparseValuesMissingKeysError(sparse_values_dict)

indices = convert_to_list(sparse_values_dict.get("indices"))
values = convert_to_list(sparse_values_dict.get("values"))

try:
return SparseValues(indices=indices, values=values, _check_type=check_type)
except TypeError:
raise SparseValuesTypeError()
5 changes: 5 additions & 0 deletions pinecone/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class PineconeProtocolError(PineconeException):
class PineconeConfigurationError(PineconeException):
"""Raised when a configuration error occurs."""

class ListConversionException(PineconeException, TypeError):
def __init__(self, message):
super().__init__(message)

__all__ = [
"PineconeConfigurationError",
"PineconeProtocolError",
Expand All @@ -30,4 +34,5 @@ class PineconeConfigurationError(PineconeException):
"UnauthorizedException",
"ForbiddenException",
"ServiceException",
"ListConversionException"
]
59 changes: 59 additions & 0 deletions pinecone/grpc/sparse_values_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import numbers

from collections.abc import Mapping
from typing import Union, Dict

from ..utils import convert_to_list

from ..data import (
SparseValuesTypeError,
SparseValuesMissingKeysError,
SparseValuesDictionaryExpectedError
)

from pinecone.core.grpc.protos.vector_service_pb2 import (
SparseValues as GRPCSparseValues,
)
from pinecone import (
SparseValues as NonGRPCSparseValues
)

class SparseValuesFactory:
@staticmethod
def build(input: Union[Dict, GRPCSparseValues, NonGRPCSparseValues]) -> GRPCSparseValues:
if input is None:
return input
if isinstance(input, GRPCSparseValues):
return input
if isinstance(input, NonGRPCSparseValues):
return GRPCSparseValues(indices=input.indices, values=input.values)
if not isinstance(input, Mapping):
raise SparseValuesDictionaryExpectedError(input)
if not {"indices", "values"}.issubset(input):
raise SparseValuesMissingKeysError(input)

indices = SparseValuesFactory._convert_to_list(input.get("indices"), int)
values = SparseValuesFactory._convert_to_list(input.get("values"), float)

if len(indices) != len(values):
raise ValueError("Sparse values indices and values must have the same length")

try:
return GRPCSparseValues(indices=indices, values=values)
except TypeError as e:
raise SparseValuesTypeError() from e

@staticmethod
def _convert_to_list(input, expected_type):
try:
converted = convert_to_list(input)
except TypeError as e:
raise SparseValuesTypeError() from e

SparseValuesFactory._validate_list_items_type(converted, expected_type)
return converted

@staticmethod
def _validate_list_items_type(input, expected_type):
if len(input) > 0 and not isinstance(input[0], expected_type):
raise SparseValuesTypeError()
38 changes: 4 additions & 34 deletions pinecone/grpc/vector_factory_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
VectorDictionaryMissingKeysError,
VectorDictionaryExcessKeysError,
VectorTupleLengthError,
SparseValuesTypeError,
SparseValuesMissingKeysError,
SparseValuesDictionaryExpectedError,
MetadataDictionaryExpectedError
)
from .sparse_values_factory import SparseValuesFactory

from pinecone.core.grpc.protos.vector_service_pb2 import (
Vector as GRPCVector,
Expand Down Expand Up @@ -73,8 +71,8 @@ def _dict_to_vector(item) -> GRPCVector:
raise TypeError(f"Column `values` is expected to be a list of floats") from e

sparse_values = item.get("sparse_values")
if sparse_values and not isinstance(sparse_values, GRPCSparseValues):
item["sparse_values"] = VectorFactoryGRPC._dict_to_sparse_values(sparse_values)
if sparse_values != None and not isinstance(sparse_values, GRPCSparseValues):
item["sparse_values"] = SparseValuesFactory.build(sparse_values)

metadata = item.get("metadata")
if metadata:
Expand All @@ -90,32 +88,4 @@ def _dict_to_vector(item) -> GRPCVector:
except TypeError as e:
if not isinstance(item["values"], Iterable) or not isinstance(item["values"].__iter__().__next__(), numbers.Real):
raise TypeError(f"Column `values` is expected to be a list of floats")
raise e

@staticmethod
def _dict_to_sparse_values(sparse_values_dict: Union[Dict, GRPCSparseValues, NonGRPCSparseValues]) -> GRPCSparseValues:
if isinstance(sparse_values_dict, GRPCSparseValues):
return sparse_values_dict
if isinstance(sparse_values_dict, NonGRPCSparseValues):
return GRPCSparseValues(indices=sparse_values_dict.indices, values=sparse_values_dict.values)

if not isinstance(sparse_values_dict, Mapping):
raise SparseValuesDictionaryExpectedError(sparse_values_dict)
if not {"indices", "values"}.issubset(sparse_values_dict):
raise SparseValuesMissingKeysError(sparse_values_dict)


try:
indices = convert_to_list(sparse_values_dict.get("indices"))
except TypeError as e:
raise SparseValuesTypeError() from e

try:
values = convert_to_list(sparse_values_dict.get("values"))
except TypeError as e:
raise SparseValuesTypeError() from e

try:
return GRPCSparseValues(indices=indices, values=values)
except TypeError as e:
raise SparseValuesTypeError() from e
raise e
11 changes: 10 additions & 1 deletion pinecone/utils/convert_to_list.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from ..exceptions import ListConversionException

def convert_to_list(obj):
class_name = obj.__class__.__name__

if class_name == 'list':
return obj
elif hasattr(obj, 'tolist') and callable(getattr(obj, 'tolist')):
return obj.tolist()
elif obj is None or isinstance(obj, str) or isinstance(obj, dict):
# The string and dictionary classes in python can be passed to list()
# but they're not going to yield sensible results for our use case.
raise ListConversionException(f"Expected a list or list-like data structure, but got: {obj}")
else:
return list(obj)
try:
return list(obj)
except Exception as e:
raise ListConversionException(f"Expected a list or list-like data structure, but got: {obj}") from e