Skip to content

Fix Bit constructor for NumPy arrays on NumPy >= 1.24 (np.bool removal) - #158

Merged
ankane merged 1 commit into
pgvector:masterfrom
HUIIIM:fix/bit-np-bool-numpy-124
Oct 2, 2026
Merged

ankane merged 1 commit into
pgvector:masterfrom
HUIIIM:fix/bit-np-bool-numpy-124

Conversation

@HUIIIM

@HUIIIM HUIIIM commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

— vertciti AI team (assisting Jiahui Miao)

Summary

Bit(np.array([True, False, True])) raises AttributeError: module 'numpy' has no attribute 'bool' on NumPy >= 1.24, because pgvector/bit.py compares value.dtype against np.bool, which was removed in NumPy 1.24 (deprecated since 1.20). One-line fix: compare against np.bool_ instead.

Reproduction

import numpy as np
from pgvector import Bit
Bit(np.array([True, False, True]))
# AttributeError: module 'numpy' has no attribute 'bool'.

Root cause

In the np.ndarray branch of Bit.__init__, if value.dtype != np.bool: — np.bool no longer exists on NumPy >= 1.24, so attribute access itself raises before any comparison happens. The other constructors (list/str/bytes) and to_numpy() are unaffected.

Fix

-            if value.dtype != np.bool:
+            if value.dtype != np.bool_:

Rationale: np.bool_ is the NumPy scalar type NumPy's own migration guidance points to. np.dtype(bool) == np.dtype(np.bool_), so the comparison semantics are unchanged — this only restores the pre-1.24 behavior, no behavior change.

Note: np.bool also appears in the type annotations in the same file, but the module uses from __future__ import annotations, so those are lazily evaluated and never raise at runtime — left untouched to keep the PR minimal. Happy to fix the annotations too if preferred.

Verification performed

  • Bit(np.array([True, False, True])) works after the fix
  • np.uint8 0/1 arrays still accepted (the np.unpackbits compatibility path)
  • np.uint8 arrays with values > 1 and float64 arrays still raise ValueError
  • 2-D arrays still raise ValueError
  • Roundtrip Bit(np.array([True, False, True])).to_numpy().tolist() == [True, False, True]
  • list/str/bytes constructors unaffected

Regression suggestion

Consider adding a test guarding the dtype comparison across the supported NumPy range, e.g. asserting Bit(np.array([True, False, True])).to_numpy().tolist() == [True, False, True].

@ankane
ankane merged commit 86f1c40 into pgvector:master Oct 2, 2026
@ankane

ankane commented Oct 2, 2026

Copy link
Copy Markdown
Member

Thanks @HUIIIM. fwiw, numpy.bool was restored in NumPy 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants