Skip to content

Commit 5bc9128

Browse files
committed
Improved errors for Bit and reduced dependency on NumPy [skip ci]
1 parent e065b87 commit 5bc9128

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

pgvector/bit.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int], np.d
1010
length = 8 * len(value)
1111
data = value
1212
else:
13+
if isinstance(value, list):
14+
def bit_value(v: bool) -> str:
15+
if v is True:
16+
return '1'
17+
if v is False:
18+
return '0'
19+
raise ValueError('expected list[bool]')
20+
21+
value = ''.join([bit_value(v) for v in value])
22+
1323
if isinstance(value, str):
1424
length = len(value)
1525

@@ -20,12 +30,7 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int], np.d
2030
data = int(value, 2).to_bytes(len(value) // 8, byteorder='big')
2131
except ValueError:
2232
raise ValueError('expected bit string')
23-
else:
24-
value = np.asarray(value)
25-
26-
# for mypy
27-
assert isinstance(value, np.ndarray)
28-
33+
elif isinstance(value, np.ndarray):
2934
if value.dtype != np.bool:
3035
# skip warning for result of np.unpackbits
3136
if value.dtype != np.uint8 or np.any(value > 1):
@@ -40,6 +45,8 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int], np.d
4045

4146
length = len(value)
4247
data = np.packbits(value).tobytes()
48+
else:
49+
raise ValueError('expected bytes, str, list, or ndarray')
4350

4451
self._value = pack('>i', length) + data
4552

tests/test_bit.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ def test_list(self):
88
assert Bit([True, False, True]).to_list() == [True, False, True]
99

1010
def test_list_none(self):
11-
with pytest.warns(UserWarning, match='expected elements to be boolean'):
12-
assert Bit([True, None, True]).to_text() == '101' # ty: ignore[invalid-argument-type]
11+
with pytest.raises(ValueError) as error:
12+
Bit([True, None, True]) # ty: ignore[invalid-argument-type]
13+
assert str(error.value) == 'expected list[bool]'
1314

1415
def test_list_int(self):
15-
with pytest.warns(UserWarning, match='expected elements to be boolean'):
16-
assert Bit([254, 7, 0]).to_text() == '110' # ty: ignore[invalid-argument-type]
16+
with pytest.raises(ValueError) as error:
17+
Bit([254, 7, 0]) # ty: ignore[invalid-argument-type]
18+
assert str(error.value) == 'expected list[bool]'
1719

1820
def test_str(self):
1921
assert Bit('101').to_list() == [True, False, True]
@@ -49,12 +51,12 @@ def test_ndarray_uint16(self):
4951
def test_ndim_two(self):
5052
with pytest.raises(ValueError) as error:
5153
Bit([[True, False], [True, False]]) # ty: ignore[invalid-argument-type]
52-
assert str(error.value) == 'expected ndim to be 1'
54+
assert str(error.value) == 'expected list[bool]'
5355

5456
def test_ndim_zero(self):
5557
with pytest.raises(ValueError) as error:
5658
Bit(True) # ty: ignore[invalid-argument-type]
57-
assert str(error.value) == 'expected ndim to be 1'
59+
assert str(error.value) == 'expected bytes, str, list, or ndarray'
5860

5961
def test_repr(self):
6062
assert repr(Bit([True, False, True])) == 'Bit(101)'

0 commit comments

Comments
 (0)