From d5c4cefbda0934ddd0a495b1406bdd4a6e910daf Mon Sep 17 00:00:00 2001 From: Zelys Date: Thu, 2 Apr 2026 16:19:06 -0500 Subject: [PATCH] fix(types): accept Sequence[float] for vector values in VectorTuple VectorTuple and VectorTupleWithMetadata were typed as tuple[str, list[float]], which caused type-checker errors when passing array[float] or other sequence types that are valid at runtime. Change list[float] to Sequence[float] so that array[float], np.ndarray, and any other float sequence are accepted without a type error. The runtime behaviour is unchanged: VectorFactory already calls convert_to_list() on the values before forwarding them to the API. Fixes #511 --- pinecone/db_data/types/vector_tuple.py | 6 ++-- tests/unit/db_data/test_vector_factory.py | 38 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/unit/db_data/test_vector_factory.py diff --git a/pinecone/db_data/types/vector_tuple.py b/pinecone/db_data/types/vector_tuple.py index 304ca9a52..477147cfa 100644 --- a/pinecone/db_data/types/vector_tuple.py +++ b/pinecone/db_data/types/vector_tuple.py @@ -1,4 +1,6 @@ +from collections.abc import Sequence + from .vector_metadata_dict import VectorMetadataTypedDict -VectorTuple = tuple[str, list[float]] -VectorTupleWithMetadata = tuple[str, list[float], VectorMetadataTypedDict] +VectorTuple = tuple[str, Sequence[float]] +VectorTupleWithMetadata = tuple[str, Sequence[float], VectorMetadataTypedDict] diff --git a/tests/unit/db_data/test_vector_factory.py b/tests/unit/db_data/test_vector_factory.py new file mode 100644 index 000000000..3b6c08cc4 --- /dev/null +++ b/tests/unit/db_data/test_vector_factory.py @@ -0,0 +1,38 @@ +"""Unit tests for VectorFactory in the db_data module.""" + +from array import array + +import pytest + +from pinecone.db_data.vector_factory import VectorFactory + + +class TestVectorFactoryTupleInput: + """Tests for VectorFactory.build() with tuple input.""" + + @pytest.mark.parametrize( + "values", + [ + [0.1, 0.2, 0.3], + array("f", [0.1, 0.2, 0.3]), + ], + ) + def test_build_tuple_two_values(self, values): + """VectorFactory accepts list[float] and array[float] in a two-element tuple.""" + result = VectorFactory.build(("id1", values)) + assert result.id == "id1" + assert len(result.values) == 3 + + @pytest.mark.parametrize( + "values", + [ + [0.1, 0.2, 0.3], + array("f", [0.1, 0.2, 0.3]), + ], + ) + def test_build_tuple_three_values(self, values): + """VectorFactory accepts list[float] and array[float] in a three-element tuple.""" + result = VectorFactory.build(("id1", values, {"genre": "comedy"})) + assert result.id == "id1" + assert len(result.values) == 3 + assert result.metadata == {"genre": "comedy"}