Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.5.2 (unreleased)

- Fixed error with empty `Bit` values
- Fixed equality for `HalfVector` with signed zeros

## 0.5.1 (2026-10-08)
Expand Down
6 changes: 5 additions & 1 deletion pgvector/bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ def __init__(
value += '0' * (8 - (length % 8))

self._length = length
self._data = int(value, 2).to_bytes(len(value) // 8, byteorder='big')
# int('', 2) raises, but a zero-length bit string is valid
if length == 0:
self._data = b''
else:
self._data = int(value, 2).to_bytes(len(value) // 8, byteorder='big')
elif is_ndarray(value):
import numpy as np

Expand Down
16 changes: 16 additions & 0 deletions tests/test_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ def test_bytes(self) -> None:
assert Bit(b'\xff\x00\xf0').to_text() == '111111110000000011110000'
assert Bit(b'\xfe\x07\x00').to_text() == '111111100000011100000000'

def test_empty(self) -> None:
bit = Bit(b'')
assert bit.to_list() == []
assert bit.to_text() == ''
assert bit.to_binary() == pack('>i', 0)
assert Bit('') == bit
assert Bit([]) == bit
assert Bit.from_text('') == bit
assert Bit.from_text(bit.to_text()) == bit
assert Bit.from_binary(bit.to_binary()) == bit
assert bit != Bit('0')
assert bit != Bit(b'\x00')
if np is not None:
assert np.array_equal(bit.to_numpy(), np.array([], dtype=bool))
assert Bit(np.array([], dtype=bool)) == bit

def test_ndarray(self) -> None:
if np is None:
pytest.skip('NumPy required')
Expand Down