Title: Bit() accepts strings Postgres rejects and silently changes the bits · Issue #161 · pgvector/pgvector-python · GitHub
Open Graph Title: Bit() accepts strings Postgres rejects and silently changes the bits · Issue #161 · pgvector/pgvector-python
X Title: Bit() accepts strings Postgres rejects and silently changes the bits · Issue #161 · pgvector/pgvector-python
Description: Bit() builds its bits with int(value, 2) at pgvector/bit.py:36, and takes _length from the raw string before that. Python's int accepts underscores, a sign, surrounding whitespace and a 0b prefix, so four strings Postgres rejects come ba...
Open Graph Description: Bit() builds its bits with int(value, 2) at pgvector/bit.py:36, and takes _length from the raw string before that. Python's int accepts underscores, a sign, surrounding whitespace and a 0b prefix, ...
X Description: Bit() builds its bits with int(value, 2) at pgvector/bit.py:36, and takes _length from the raw string before that. Python's int accepts underscores, a sign, surrounding whitespace and a 0b pref...
Opengraph URL: https://github.com/pgvector/pgvector-python/issues/161
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bit() accepts strings Postgres rejects and silently changes the bits","articleBody":"`Bit()` builds its bits with `int(value, 2)` at `pgvector/bit.py:36`, and takes `_length` from the raw string before that. Python's `int` accepts underscores, a sign, surrounding whitespace and a `0b` prefix, so four strings Postgres rejects come back as a `Bit` holding different bits and a different length.\n\n```\n$ PYTHONPATH=/var/tmp/p11L1/pgv python pgv_bit_probe.py # master fd74f78, Python 3.12.3\npgvector: /var/tmp/p11L1/pgv/pgvector/bit.py\nBit('1010' ) -> to_text()='1010' len=4\nBit('1010_1010' ) -> to_text()='010101010' len=9\nBit('+1010' ) -> to_text()='01010' len=5\nBit(' 1010' ) -> to_text()='01010' len=5\nBit('1010\\n' ) -> ValueError: expected bit string\nBit('-1010' ) -> OverflowError: can't convert negative int to unsigned\nBit('0b1010' ) -> to_text()='001010' len=6\n```\n\nPostgres rejects all four literals, but the client never sends them. What it sends in their place is a valid bit string, and the column takes it. pgvector 0.8.7 on PostgreSQL 17.11, `docker.io/pgvector/pgvector:pg17`:\n\n```\npostgres=# SELECT '1010_1010'::bit(9);\nERROR: \"_\" is not a valid binary digit\npostgres=# SELECT '0b1010'::bit(6);\nERROR: \"b\" is not a valid binary digit\npostgres=# SELECT '01010'::bit(5); -- what Bit('+1010') sends instead\n 01010\n```\n\n`'-1010'` raises `OverflowError` where the `except ValueError` at `bit.py:37-38` was meant to turn it into `expected bit string`.\n\nA `re.fullmatch('[01]*', value)` before the padding rejects all five and times the same as the `int()` it guards. The obvious `set(value) \u003c= {'0', '1'}` is 4.6x, which is worth avoiding on a constructor you already tuned in `2ae28ba`:\n\n```\n$ python3 -m timeit -s \"import re; s='01'*32000\" \"\u003cexpr>\" # Python 3.12.3, best of 5\nint(s, 2) 98.4 usec\nre.fullmatch('[01]*', s) 92.9 usec\nset(s) \u003c= {'0','1'} 451 usec\n```\n\n@ankane would you take a pull request for that, with cases in `tests/test_bit.py` next to `test_str`, or do you read the permissive parsing here as deliberate?\n","author":{"url":"https://github.com/chrikrah","@type":"Person","name":"chrikrah"},"datePublished":"2026-10-04T05:02:20Z","url":"https://github.com/pgvector/pgvector-python/issues/161"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:03d16ac3-2c20-b89a-4900-e29d0012ecb3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E4A2:3C86C1:C4D5AB:102251B:6AFE8A68 |
| html-safe-nonce | dba61e84c5bc08a7a80bfd6cc3a4de3c7f21682d4e6915ff8e6af6707fff30f9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNEEyOjNDODZDMTpDNEQ1QUI6MTAyMjUxQjo2QUZFOEE2OCIsInZpc2l0b3JfaWQiOiI4NjEyMTY2MzYyNDI2NDExNjI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 883fc3c707ed05956a1acd90b992eb1d8ff7bba3cb404506c5c323ab630a008b |
| hovercard-subject-tag | issue:5696406139 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pgvector/pgvector-python/161/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3285442c5f76957e1238c7655381c03efdd8f409609c5668bd471fb62d776bc3/pgvector/pgvector-python/issues/161 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3285442c5f76957e1238c7655381c03efdd8f409609c5668bd471fb62d776bc3/pgvector/pgvector-python/issues/161 |
| og:image:alt | Bit() builds its bits with int(value, 2) at pgvector/bit.py:36, and takes _length from the raw string before that. Python's int accepts underscores, a sign, surrounding whitespace and a 0b prefix, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrikrah |
| hostname | github.com |
| expected-hostname | github.com |
| None | b3c0f155eafc27e60db6e254d1ffff0e072e9ab3b4018a5ee518797c19d89f86 |
| turbo-cache-control | no-preview |
| go-import | github.com/pgvector/pgvector-python git https://github.com/pgvector/pgvector-python.git |
| octolytics-dimension-user_id | 98363230 |
| octolytics-dimension-user_login | pgvector |
| octolytics-dimension-repository_id | 376300726 |
| octolytics-dimension-repository_nwo | pgvector/pgvector-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 376300726 |
| octolytics-dimension-repository_network_root_nwo | pgvector/pgvector-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4a10310af51a1bfffbedc4fd81cb582acf0cb0ff |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width