Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pinecone/grpc/pinecone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pinecone import Pinecone
from pinecone.config import ConfigBuilder
from pinecone.utils import normalize_host
from pinecone.pinecone import check_realistic_host
from .index_grpc import GRPCIndex


Expand Down Expand Up @@ -122,8 +124,13 @@ def Index(self, name: str = "", host: str = "", **kwargs):
if name == "" and host == "":
raise ValueError("Either name or host must be specified")

# Use host if it is provided, otherwise get host from describe_index
index_host = host or self.db.index._get_host(name)
if host != "":
check_realistic_host(host)
# Use host url if it is provided
index_host = normalize_host(host)
else:
# Otherwise, get host url from describe_index using the index name
index_host = self.db.index._get_host(name)

pt = kwargs.pop("pool_threads", None) or self._pool_threads

Expand Down
26 changes: 24 additions & 2 deletions tests/unit_grpc/test_grpc_index_initialization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import re
from pinecone.grpc import PineconeGRPC, GRPCClientConfig

Expand Down Expand Up @@ -73,7 +74,7 @@ def test_config_passed_when_target_by_host(self):
def test_config_passed_when_target_by_host_and_port(self):
pc = PineconeGRPC(api_key="YOUR_API_KEY")
config = GRPCClientConfig(timeout=5, secure=False)
index = pc.Index(host="myhost:4343", grpc_config=config)
index = pc.Index(host="myhost.example.com:4343", grpc_config=config)

assert index.grpc_client_config.timeout == 5
assert index.grpc_client_config.secure == False
Expand All @@ -83,11 +84,32 @@ def test_config_passed_when_target_by_host_and_port(self):
assert index.grpc_client_config.conn_timeout == 1

# Endpoint calculation does not override port
assert index._endpoint() == "myhost:4343"
assert index._endpoint() == "myhost.example.com:4343"

def test_config_passes_source_tag_when_set(self):
pc = PineconeGRPC(api_key="YOUR_API_KEY", source_tag="my_source_tag")
assert (
re.search(r"source_tag=my_source_tag", pc.db._index_api.api_client.user_agent)
is not None
)

def test_invalid_host(self):
pc = PineconeGRPC(api_key="key")

with pytest.raises(ValueError) as e:
pc.Index(host="invalid")
assert "You passed 'invalid' as the host but this does not appear to be valid" in str(
e.value
)

with pytest.raises(ValueError) as e:
pc.Index(host="my-index")
assert "You passed 'my-index' as the host but this does not appear to be valid" in str(
e.value
)

# Can instantiate with realistic host
pc.Index(host="test-bt8x3su.svc.apw5-4e34-81fa.pinecone.io")

# Can instantiate with localhost address
pc.Index(host="localhost:8080")
Loading