Skip to content

Commit db122fe

Browse files
committed
Added check for ndim for Bit [skip ci]
1 parent 7e17e9e commit db122fe

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pgvector/utils/bit.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ def __init__(self, value):
99
elif isinstance(value, str):
1010
self._value = __class__.from_text(value)._value
1111
else:
12-
self._value = np.asarray(value, dtype=bool)
12+
value = np.asarray(value, dtype=bool)
13+
14+
if value.ndim != 1:
15+
raise ValueError('expected ndim to be 1')
16+
17+
self._value = value
1318

1419
def __str__(self):
1520
return self.to_text()

tests/test_bit.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ def test_ndarray_same_object(self):
1818
assert Bit(arr).to_list() == [True, False, True]
1919
assert Bit(arr).to_numpy() is arr
2020

21+
def test_ndim_two(self):
22+
with pytest.raises(ValueError) as error:
23+
Bit([[True, False], [True, False]])
24+
assert str(error.value) == 'expected ndim to be 1'
25+
26+
def test_ndim_zero(self):
27+
with pytest.raises(ValueError) as error:
28+
Bit(True)
29+
assert str(error.value) == 'expected ndim to be 1'
30+
2131
def test_repr(self):
2232
assert repr(Bit([True, False, True])) == 'Bit(101)'

0 commit comments

Comments
 (0)