From cf42bbf1b3d2207b6cb64e3f2782cbd8dcf8cb16 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 14 Jan 2024 21:52:04 -0500 Subject: [PATCH 1/5] Adjust signature of Index() --- pinecone/control/pinecone.py | 16 ++++++++++++---- pinecone/grpc/pinecone.py | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index 97430fe1d..c0a2e888c 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -213,7 +213,15 @@ def _get_status(self, name: str): response = api_instance.describe_index(name) return response["status"] - def Index(self, name: str, host: Optional[str] = None): - if host is None: - host = self.index_host_store.get_host(self.index_api, self.config, name) - return Index(api_key=self.config.api_key, host=host, pool_threads=self.pool_threads) + def Index(self, name: str = '', host: str = ''): + if name == '' and host == '': + raise ValueError("Either name or host must be specified") + + if host != '': + # Use host url if it is provided + return Index(api_key=self.config.api_key, host=host, pool_threads=self.pool_threads) + + if name != '': + # Otherwise, get host url from describe_index using the index name + index_host = self.index_host_store.get_host(self.index_api, self.config, name) + return Index(api_key=self.config.api_key, host=index_host, pool_threads=self.pool_threads) diff --git a/pinecone/grpc/pinecone.py b/pinecone/grpc/pinecone.py index 378c07631..987fe3ea0 100644 --- a/pinecone/grpc/pinecone.py +++ b/pinecone/grpc/pinecone.py @@ -4,8 +4,17 @@ from typing import Optional class PineconeGRPC(Pinecone): - def Index(self, name: str, host: Optional[str] = None): - if host is None: - host = self.index_host_store.get_host(self.index_api, self.config, name) - config = ConfigBuilder.build(api_key=self.config.api_key, host=host) - return GRPCIndex(index_name=name, config=config) + def Index(self, name: str = '', host: str = ''): + if name == '' and host == '': + raise ValueError("Either name or host must be specified") + + if host != '': + # Use host url if it is provided + config = ConfigBuilder.build(api_key=self.config.api_key, host=host) + return GRPCIndex(index_name=name, config=config) + + if name != '': + # Otherwise, get host url from describe_index using the index name + index_host = self.index_host_store.get_host(self.index_api, self.config, name) + config = ConfigBuilder.build(api_key=self.config.api_key, host=index_host) + return GRPCIndex(index_name=name, config=config) From 5a4dbf6f818de44187a40b46ef0ff474945b3e0f Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 14 Jan 2024 22:16:32 -0500 Subject: [PATCH 2/5] Add integration tests --- pinecone/control/pinecone.py | 4 +-- pinecone/grpc/pinecone.py | 3 +- tests/integration/data/test_initialization.py | 34 +++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/integration/data/test_initialization.py diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index c0a2e888c..02752571f 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -7,7 +7,7 @@ from pinecone.core.client.api.manage_indexes_api import ManageIndexesApi as IndexOperationsApi from pinecone.core.client.api_client import ApiClient -from pinecone.utils import get_user_agent +from pinecone.utils import get_user_agent, normalize_host from pinecone.core.client.models import ( CreateCollectionRequest, CreateIndexRequest, @@ -219,7 +219,7 @@ def Index(self, name: str = '', host: str = ''): if host != '': # Use host url if it is provided - return Index(api_key=self.config.api_key, host=host, pool_threads=self.pool_threads) + return Index(api_key=self.config.api_key, host=normalize_host(host), pool_threads=self.pool_threads) if name != '': # Otherwise, get host url from describe_index using the index name diff --git a/pinecone/grpc/pinecone.py b/pinecone/grpc/pinecone.py index 987fe3ea0..7279b1078 100644 --- a/pinecone/grpc/pinecone.py +++ b/pinecone/grpc/pinecone.py @@ -1,7 +1,6 @@ from ..control.pinecone import Pinecone from ..config.config import ConfigBuilder from .index_grpc import GRPCIndex -from typing import Optional class PineconeGRPC(Pinecone): def Index(self, name: str = '', host: str = ''): @@ -9,7 +8,7 @@ def Index(self, name: str = '', host: str = ''): raise ValueError("Either name or host must be specified") if host != '': - # Use host url if it is provided + # Use host if it is provided config = ConfigBuilder.build(api_key=self.config.api_key, host=host) return GRPCIndex(index_name=name, config=config) diff --git a/tests/integration/data/test_initialization.py b/tests/integration/data/test_initialization.py new file mode 100644 index 000000000..0e709cf09 --- /dev/null +++ b/tests/integration/data/test_initialization.py @@ -0,0 +1,34 @@ +import pytest + +class TestIndexClientInitialization: + def test_index_direct_host_kwarg(client, index_host): + index = client.Index(host=index_host) + index.find(ids=['1', '2', '3']) + + def test_index_direct_host_with_https(client, index_host): + if not index_host.startswith('https://'): + index_host = 'https://' + index_host + index = client.Index(index_host) + index.find(ids=['1', '2', '3']) + + def test_index_direct_host_without_http(client, index_host): + if index_host.startswith('https://'): + index_host = index_host[8:] + index = client.Index(host=index_host) + index.find(ids=['1', '2', '3']) + + def test_index_by_name_positional(client, index_name): + index = client.Index(index_name) + index.find(ids=['1', '2', '3']) + + def test_index_by_name_positional_with_host(client, index_name, index_host): + index = client.Index(index_name, index_host) + index.find(ids=['1', '2', '3']) + + def test_index_by_name_kwargs(client, index_name): + index = client.Index(name=index_name) + index.find(ids=['1', '2', '3']) + + def test_index_by_name_kwargs_with_host(client, index_name, index_host): + index = client.Index(name=index_name, host=index_host) + index.find(ids=['1', '2', '3']) \ No newline at end of file From ea0cb97746813faa5c3a5473ceda4304f4ee08cf Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 14 Jan 2024 22:21:01 -0500 Subject: [PATCH 3/5] Fix tests --- tests/integration/data/test_initialization.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/integration/data/test_initialization.py b/tests/integration/data/test_initialization.py index 0e709cf09..c25539dbf 100644 --- a/tests/integration/data/test_initialization.py +++ b/tests/integration/data/test_initialization.py @@ -1,34 +1,38 @@ import pytest class TestIndexClientInitialization: - def test_index_direct_host_kwarg(client, index_host): + def test_index_direct_host_kwarg(self, client, index_host): index = client.Index(host=index_host) index.find(ids=['1', '2', '3']) - def test_index_direct_host_with_https(client, index_host): + def test_index_direct_host_with_https(self, client, index_host): if not index_host.startswith('https://'): index_host = 'https://' + index_host index = client.Index(index_host) index.find(ids=['1', '2', '3']) - def test_index_direct_host_without_http(client, index_host): + def test_index_direct_host_without_http(self, client, index_host): if index_host.startswith('https://'): index_host = index_host[8:] index = client.Index(host=index_host) index.find(ids=['1', '2', '3']) - def test_index_by_name_positional(client, index_name): + def test_index_by_name_positional(self, client, index_name): index = client.Index(index_name) index.find(ids=['1', '2', '3']) - def test_index_by_name_positional_with_host(client, index_name, index_host): + def test_index_by_name_positional_with_host(self, client, index_name, index_host): index = client.Index(index_name, index_host) index.find(ids=['1', '2', '3']) - def test_index_by_name_kwargs(client, index_name): + def test_index_by_name_kwargs(self, client, index_name): index = client.Index(name=index_name) index.find(ids=['1', '2', '3']) - def test_index_by_name_kwargs_with_host(client, index_name, index_host): + def test_index_by_name_kwargs_with_host(self, client, index_name, index_host): index = client.Index(name=index_name, host=index_host) - index.find(ids=['1', '2', '3']) \ No newline at end of file + index.find(ids=['1', '2', '3']) + + def test_raises_when_no_name_or_host(self, client): + with pytest.raises(ValueError): + client.Index() \ No newline at end of file From 48bf74d33a7355e825064d38f8f034220af59291 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 14 Jan 2024 22:24:35 -0500 Subject: [PATCH 4/5] Fix typo: find -> fetch --- tests/integration/data/test_initialization.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/integration/data/test_initialization.py b/tests/integration/data/test_initialization.py index c25539dbf..21663f194 100644 --- a/tests/integration/data/test_initialization.py +++ b/tests/integration/data/test_initialization.py @@ -3,35 +3,35 @@ class TestIndexClientInitialization: def test_index_direct_host_kwarg(self, client, index_host): index = client.Index(host=index_host) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_direct_host_with_https(self, client, index_host): if not index_host.startswith('https://'): index_host = 'https://' + index_host index = client.Index(index_host) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_direct_host_without_http(self, client, index_host): if index_host.startswith('https://'): index_host = index_host[8:] index = client.Index(host=index_host) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_by_name_positional(self, client, index_name): index = client.Index(index_name) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_by_name_positional_with_host(self, client, index_name, index_host): index = client.Index(index_name, index_host) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_by_name_kwargs(self, client, index_name): index = client.Index(name=index_name) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_index_by_name_kwargs_with_host(self, client, index_name, index_host): index = client.Index(name=index_name, host=index_host) - index.find(ids=['1', '2', '3']) + index.fetch(ids=['1', '2', '3']) def test_raises_when_no_name_or_host(self, client): with pytest.raises(ValueError): From 7fe843a6a148b080e13f4282293917aae8cffa32 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Sun, 14 Jan 2024 22:41:56 -0500 Subject: [PATCH 5/5] Fix typo in test --- tests/integration/data/test_initialization.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration/data/test_initialization.py b/tests/integration/data/test_initialization.py index 21663f194..06273f0ad 100644 --- a/tests/integration/data/test_initialization.py +++ b/tests/integration/data/test_initialization.py @@ -8,16 +8,16 @@ def test_index_direct_host_kwarg(self, client, index_host): def test_index_direct_host_with_https(self, client, index_host): if not index_host.startswith('https://'): index_host = 'https://' + index_host - index = client.Index(index_host) + index = client.Index(host=index_host) index.fetch(ids=['1', '2', '3']) - def test_index_direct_host_without_http(self, client, index_host): + def test_index_direct_host_without_https(self, client, index_host): if index_host.startswith('https://'): index_host = index_host[8:] index = client.Index(host=index_host) index.fetch(ids=['1', '2', '3']) - def test_index_by_name_positional(self, client, index_name): + def test_index_by_name_positional_only(self, client, index_name, index_host): index = client.Index(index_name) index.fetch(ids=['1', '2', '3']) @@ -33,6 +33,6 @@ def test_index_by_name_kwargs_with_host(self, client, index_name, index_host): index = client.Index(name=index_name, host=index_host) index.fetch(ids=['1', '2', '3']) - def test_raises_when_no_name_or_host(self, client): + def test_raises_when_no_name_or_host(self, client, index_host): with pytest.raises(ValueError): client.Index() \ No newline at end of file