Skip to content

Commit 2d1b754

Browse files
committed
Restored backwards compatibility of Bit constructor
1 parent 534ec18 commit 2d1b754

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

pgvector/bit.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from struct import pack, unpack_from
3+
from warnings import warn
34

45

56
class Bit:
@@ -9,15 +10,11 @@ def __init__(self, value):
910
elif isinstance(value, bytes):
1011
self._value = np.unpackbits(np.frombuffer(value, dtype=np.uint8)).astype(bool)
1112
else:
12-
if isinstance(value, np.ndarray):
13-
if value.dtype == np.uint8:
14-
value = np.unpackbits(value).astype(bool)
15-
elif value.dtype != np.bool:
16-
raise ValueError('expected dtype to be bool or uint8')
17-
else:
18-
value = np.asarray(value)
19-
if value.dtype != np.bool:
20-
raise ValueError('expected all elements to be boolean')
13+
value = np.asarray(value)
14+
15+
if value.dtype != np.bool:
16+
warn('expected elements to be boolean', stacklevel=2)
17+
value = value.astype(bool)
2118

2219
if value.ndim != 1:
2320
raise ValueError('expected ndim to be 1')

tests/test_bit.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ 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.raises(ValueError) as error:
12-
Bit([True, None, True])
13-
assert str(error.value) == 'expected all elements to be boolean'
11+
with pytest.warns(UserWarning, match='expected elements to be boolean'):
12+
assert Bit([True, None, True]).to_text() == '101'
1413

1514
def test_list_int(self):
16-
with pytest.raises(ValueError) as error:
17-
Bit([254, 7, 0])
18-
assert str(error.value) == 'expected all elements to be boolean'
15+
with pytest.warns(UserWarning, match='expected elements to be boolean'):
16+
assert Bit([254, 7, 0]).to_text() == '110'
1917

2018
def test_tuple(self):
2119
assert Bit((True, False, True)).to_list() == [True, False, True]
@@ -29,13 +27,13 @@ def test_bytes(self):
2927

3028
def test_ndarray_uint8(self):
3129
arr = np.array([254, 7, 0], dtype=np.uint8)
32-
assert Bit(arr).to_text() == '111111100000011100000000'
30+
with pytest.warns(UserWarning, match='expected elements to be boolean'):
31+
assert Bit(arr).to_text() == '110'
3332

3433
def test_ndarray_uint16(self):
3534
arr = np.array([254, 7, 0], dtype=np.uint16)
36-
with pytest.raises(ValueError) as error:
37-
Bit(arr)
38-
assert str(error.value) == 'expected dtype to be bool or uint8'
35+
with pytest.warns(UserWarning, match='expected elements to be boolean'):
36+
assert Bit(arr).to_text() == '110'
3937

4038
def test_ndarray_same_object(self):
4139
arr = np.array([True, False, True])

0 commit comments

Comments
 (0)