Skip to content

Commit ba94991

Browse files
committed
Added support for empty vectors to from_text methods [skip ci]
1 parent 90ac931 commit ba94991

4 files changed

Lines changed: 6 additions & 6 deletions

File tree

‎pgvector/halfvec.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def from_binary(cls, value: bytes | bytearray | memoryview, /) -> HalfVector:
8888

8989
@classmethod
9090
def _from_text(cls, value: str, /) -> list[float]:
91+
if len(value) == 2:
92+
return []
9193
return [float(v) for v in value[1:-1].split(',')]
9294

9395
@classmethod

‎pgvector/vector.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def from_binary(cls, value: bytes | bytearray | memoryview, /) -> Vector:
8686

8787
@classmethod
8888
def _from_text(cls, value: str, /) -> list[float]:
89+
if len(value) == 2:
90+
return []
8991
return [float(v) for v in value[1:-1].split(',')]
9092

9193
@classmethod

‎tests/test_half_vector.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ def test_from_text(self) -> None:
6868
assert vec.to_text() == '[1.5,2.0,3.0]'
6969

7070
def test_from_text_zero_dimensions(self) -> None:
71-
# TODO no error
72-
with pytest.raises(ValueError, match='could not convert string to float'):
73-
HalfVector.from_text('[]')
71+
assert HalfVector.from_text('[]') == HalfVector([])
7472

7573
def test_from_text_open_bracket(self) -> None:
7674
# TODO raise ValueError in 0.6.0

‎tests/test_vector.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ def test_from_text(self) -> None:
6868
assert vec.to_text() == '[1.5,2.0,3.0]'
6969

7070
def test_from_text_zero_dimensions(self) -> None:
71-
# TODO no error
72-
with pytest.raises(ValueError, match='could not convert string to float'):
73-
Vector.from_text('[]')
71+
assert Vector.from_text('[]') == Vector([])
7472

7573
def test_from_text_open_bracket(self) -> None:
7674
# TODO raise ValueError in 0.6.0

0 commit comments

Comments
 (0)