Repository navigation
Added support for Bit objects with Psycopg 2 and pg8000 - #170
Open
astroknot-sheep wants to merge 1 commit into
Open
astroknot-sheep wants to merge 1 commit into
astroknot-sheep wants to merge 1 commit into
Conversation
chrikrah
approved these changes
Oct 28, 2026
chrikrah
left a comment
There was a problem hiding this comment.
@astroknot-sheep this works on both drivers and brings them in line with Psycopg 3, which already adapts Bit. Reverting both register.py files fails the new tests with the #169 errors, and <~>, = and & resolve against a Bit on both drivers.
# Postgres 17 + pgvector (docker.io/pgvector/pgvector:pg17), CPython 3.12, pgvector imported from the exported tree
$ python -m pytest tests -q -p no:cacheprovider # head 4764b5b
519 passed, 8 skipped in 29.96s
$ python -m pytest tests/test_psycopg2.py tests/test_pg8000.py -q -p no:cacheprovider -k bit_class # head, both register.py from e0f3ac3
E psycopg2.ProgrammingError: can't adapt type 'Bit'
E pg8000.exceptions.DatabaseError: {... 'M': '"i" is not a valid binary digit', ...}
2 failed, 15 deselected in 0.27s
# probe at head: SELECT '111'::bit(3) <~> p1, '1010'::varbit = p2, p3::bit(3) & '011'::bit(3)
# with p1..p3 = Bit("101"), Bit("1010"), Bit("110")
psycopg2 (1.0, True, '010')
pg8000 [[1.0, True, '010']]
# asyncpg 0.32.0 at head, after register_vector(conn)
SELECT $1::bit(3) -> DataError invalid input for query argument $1: Bit(101) (a bytes-like object is required, not 'Bit')
# not run: mypy, ty
non-blocking: asyncpg has the same gap, since its register_vector sets no codec for bit. I'd keep this PR to the two drivers #169 names and track asyncpg in its own issue. Shall I open it?
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.
Fixes #169
Bitobjects can be passed as query parameters with Psycopg 3, but not with Psycopg 2 or pg8000. This adds:pgvector/psycopg2/bit.pywith aBitAdapter, matchingHalfvecAdapter/SparsevecAdapter, registered inregister_vectorBitout adapter for pg8000 usingto_text(), alongside the existingVectoroneasyncpg is unchanged (
asyncpg.BitStringper #110).Tests:
test_bit_classfor Psycopg 2 and pg8000 (both fail on master). Full suite passes locally (527 passed, Postgres 17), plusmypyandty check.Written with AI assistance; I have reviewed and tested the changes.