Skip to content

Commit 7066ca7

Browse files
fix(python): accept NumPy scalar values in DenseVector
1 parent 33928df commit 7066ca7

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎client/python/tests/test_models.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
from dataclasses import FrozenInstanceError
2+
13
import pytest
24

5+
from vortexdb.grpc import vector_db_pb2
36
from vortexdb.models import (
7+
ContentType,
48
DenseVector,
59
Payload,
610
Point,
711
Similarity,
8-
ContentType,
912
)
1013

11-
from vortexdb.grpc import vector_db_pb2
12-
1314
# DenseVector Tests
1415

1516

@@ -31,6 +32,13 @@ def test_dense_vector_accepts_numpy_array():
3132
assert v.values == [1.0, 2.5, 3.0]
3233

3334

35+
def test_dense_vector_accepts_numpy_scalars_in_list():
36+
numpy = pytest.importorskip("numpy")
37+
v = DenseVector([numpy.float32(1.5), numpy.float32(2.5)])
38+
assert v.values == [1.5, 2.5]
39+
assert all(type(value) is float for value in v.values)
40+
41+
3442
def test_dense_vector_accepts_pytorch_tensor():
3543
torch = pytest.importorskip("torch")
3644
v = DenseVector(torch.tensor([1, 2.5, 3]))
@@ -135,7 +143,7 @@ def test_dense_vector_rejects_multidimensional_tensor_like_value():
135143

136144
def test_dense_vector_is_frozen():
137145
v = DenseVector([1, 2, 3])
138-
with pytest.raises(Exception):
146+
with pytest.raises(FrozenInstanceError):
139147
v.values = [4, 5, 6]
140148

141149

‎client/python/vortexdb/models.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from dataclasses import dataclass
22
from enum import Enum
3-
from typing import Any, List
4-
from vortexdb.grpc import vector_db_pb2
3+
from numbers import Real
4+
from typing import Any
55

6+
from vortexdb.grpc import vector_db_pb2
67

78
# I found this to be a good idea, because
89
# 1. readability
@@ -45,7 +46,7 @@ def from_proto(value: int) -> "ContentType":
4546

4647
@dataclass(frozen=True)
4748
class DenseVector:
48-
values: List[float]
49+
values: list[float]
4950

5051
def __post_init__(self):
5152
if isinstance(self.values, (list, tuple)):
@@ -60,7 +61,7 @@ def __post_init__(self):
6061
raise ValueError("DenseVector expects a one-dimensional vector")
6162

6263
for v in normalized_values:
63-
if not isinstance(v, (int, float)):
64+
if not isinstance(v, Real):
6465
raise TypeError("DenseVector values must be numeric (int or float)")
6566

6667
object.__setattr__(self, "values", [float(v) for v in normalized_values])

0 commit comments

Comments
 (0)