diff --git a/pinecone/core/utils/__init__.py b/pinecone/core/utils/__init__.py index 64542bccc..ba8df7e41 100644 --- a/pinecone/core/utils/__init__.py +++ b/pinecone/core/utils/__init__.py @@ -26,6 +26,7 @@ def dump_numpy_public(np_array: 'np.ndarray', compressed: bool = False) -> 'vect """ Dump numpy array to vector_column_service_pb2.NdArray """ + warn_deprecated('dump_numpy_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0') protobuf_arr = vector_column_service_pb2.NdArray() protobuf_arr.dtype = str(np_array.dtype) protobuf_arr.shape.extend(np_array.shape) @@ -38,6 +39,7 @@ def dump_numpy_public(np_array: 'np.ndarray', compressed: bool = False) -> 'vect def dump_strings_public(strs: List[str], compressed: bool = False) -> 'vector_column_service_pb2.NdArray': + warn_deprecated('dump_strings_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0') return dump_numpy_public(np.array(strs, dtype='S'), compressed=compressed) @@ -90,6 +92,7 @@ def load_numpy_public(proto_arr: 'vector_column_service_pb2.NdArray') -> 'np.nda :param proto_arr: :return: """ + warn_deprecated('load_numpy_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0') if len(proto_arr.shape) == 0: return np.array([]) if proto_arr.compressed: @@ -100,9 +103,9 @@ def load_numpy_public(proto_arr: 'vector_column_service_pb2.NdArray') -> 'np.nda def load_strings_public(proto_arr: 'vector_column_service_pb2.NdArray') -> List[str]: + warn_deprecated('load_strings_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0') return [str(item, 'utf-8') for item in load_numpy_public(proto_arr)] - def warn_deprecated(description: str = '', deprecated_in: str = None, removal_in: str = None): message = f'DEPRECATED since v{deprecated_in} [Will be removed in v{removal_in}]: {description}' - warnings.warn(message, DeprecationWarning) + warnings.warn(message, FutureWarning) diff --git a/pinecone/index.py b/pinecone/index.py index 12348f491..cd6f22e6c 100644 --- a/pinecone/index.py +++ b/pinecone/index.py @@ -16,7 +16,7 @@ ScoredVector, SingleQueryResults, DescribeIndexStatsResponse, UpsertRequest, UpsertResponse, UpdateRequest, \ Vector, DeleteRequest, UpdateRequest, DescribeIndexStatsRequest from pinecone.core.client.api.vector_operations_api import VectorOperationsApi -from pinecone.core.utils import fix_tuple_length, get_user_agent +from pinecone.core.utils import fix_tuple_length, get_user_agent, warn_deprecated import copy __all__ = [ @@ -33,7 +33,6 @@ '_check_input_type', '_check_return_type', '_host_index', 'async_req' ) - def parse_query_response(response: QueryResponse, unary_query: bool): if unary_query: response._data_store.pop('results', None) @@ -42,6 +41,10 @@ def parse_query_response(response: QueryResponse, unary_query: bool): response._data_store.pop('namespace', None) return response +def upsert_numpy_deprecation_notice(context): + numpy_deprecataion_notice = "The ability to pass a numpy ndarray as part of a dictionary argument to upsert() will be removed in a future version of the pinecone client. To remove this warning, use the numpy.ndarray.tolist method to convert your ndarray into a python list before calling upsert()." + message = " ".join([context, numpy_deprecataion_notice]) + warn_deprecated(message, deprecated_in='2.2.1', removal_in='3.0.0') class Index(ApiClient): @@ -187,8 +190,10 @@ def _dict_to_vector(item): values = item['sparse_values'].get('values', None) if isinstance(values, np.ndarray): + upsert_numpy_deprecation_notice("Deprecated type passed in sparse_values['values'].") values = values.tolist() if isinstance(indices, np.ndarray): + upsert_numpy_deprecation_notice("Deprecated type passed in sparse_values['indices'].") indices = indices.tolist() try: item['sparse_values'] = SparseValues(indices=indices, values=values) @@ -203,6 +208,7 @@ def _dict_to_vector(item): raise TypeError(f"Column `metadata` is expected to be a dictionary, found {type(metadata)}") if isinstance(item['values'], np.ndarray): + upsert_numpy_deprecation_notice("Deprecated type passed in 'values'.") item['values'] = item['values'].tolist() try: diff --git a/requirements.txt b/requirements.txt index 6544a088d..9732771a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,4 @@ dnspython>=2.0.0 python_dateutil >= 2.5.3 urllib3 >= 1.21.1 tqdm >= 4.64.1 -numpy \ No newline at end of file +numpy >= 1.22.0 \ No newline at end of file diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index efb30e969..fc549646a 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -1,5 +1,7 @@ import pandas as pd +import numpy as np import pytest +import warnings from pinecone.core.client.api_client import Endpoint @@ -34,8 +36,15 @@ def setup_method(self): pinecone.init(api_key='example-key') self.index = pinecone.Index('example-name') - - # region: upsert tests + + def test_upsert_numpy_deprecation_warning(self, mocker): + mocker.patch.object(self.index._vector_api, 'upsert', autospec=True) + with pytest.warns(FutureWarning): + # numpy used in dictionary values + self.index.upsert([{'id': '2', 'values': np.array([3.0, 3.2, 4.2])}]) + with pytest.warns(FutureWarning): + # numpy used in dictionary sparse_values + self.index.upsert([{'id': '3', 'values': [0.5,0.5,0.5], 'sparse_values': { 'indices': np.array([0, 1]), 'values': np.array([3.0, 3.2])}}]) def test_upsert_tuplesOfIdVec_UpserWithoutMD(self, mocker): mocker.patch.object(self.index._vector_api, 'upsert', autospec=True)