From 9427b60dec185390a9ffd37d40f55230f38c446e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 14 Oct 2026 12:37:01 +0000 Subject: [PATCH] Fixed error with empty bit strings - fixes #165 Co-authored-by: Zaakir Hussain --- CHANGELOG.md | 1 + pgvector/bit.py | 6 +++++- tests/test_bit.py | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9561ff5..fa729ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/pgvector/bit.py b/pgvector/bit.py index a157b30..47f82f9 100644 --- a/pgvector/bit.py +++ b/pgvector/bit.py @@ -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 diff --git a/tests/test_bit.py b/tests/test_bit.py index ed13374..e4a4547 100644 --- a/tests/test_bit.py +++ b/tests/test_bit.py @@ -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')