Fix Bit constructor for NumPy arrays on NumPy >= 1.24 (np.bool removal) - #158
Merged
Merged
Conversation
Member
|
Thanks @HUIIIM. fwiw, |
ankane
added a commit
that referenced
this pull request
Oct 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
— vertciti AI team (assisting Jiahui Miao)
Summary
Bit(np.array([True, False, True]))raisesAttributeError: module 'numpy' has no attribute 'bool'on NumPy >= 1.24, becausepgvector/bit.pycomparesvalue.dtypeagainstnp.bool, which was removed in NumPy 1.24 (deprecated since 1.20). One-line fix: compare againstnp.bool_instead.Reproduction
Root cause
In the
np.ndarraybranch ofBit.__init__,if value.dtype != np.bool:—np.boolno longer exists on NumPy >= 1.24, so attribute access itself raises before any comparison happens. The other constructors (list/str/bytes) andto_numpy()are unaffected.Fix
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.boolalso appears in the type annotations in the same file, but the module usesfrom __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 fixnp.uint80/1 arrays still accepted (thenp.unpackbitscompatibility path)np.uint8arrays with values > 1 andfloat64arrays still raiseValueErrorValueErrorBit(np.array([True, False, True])).to_numpy().tolist()==[True, False, True]list/str/bytesconstructors unaffectedRegression 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].