From: vallal28 (@vallal28) <noreply+vallal28@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #981: Proposal: Enhance state integrity for HalfVector via @property
Date: Sun, 03 May 2026 18:46:16 +0000
Message-ID: <gh-pgvector-pgvector-981@github.com> (raw)
Issue Description:
Hi there,
I’ve been diving into the pgvector Python client and noticed a potential improvement regarding how HalfVector manages its internal state.
The Observation:
Currently, the validation for dimensions (ndim) and data type (dtype) is performed strictly within the __init__ method. While this ensures the object is created correctly, the internal _value attribute can be modified or overwritten post-initialization without re-triggering these guards.
For example:
Python
vec = HalfVector([1.0, 2.0])
# This bypasses all validation and leaves the object in an inconsistent state
vec._value = np.array([[1, 2], [3, 4]])
Proposed Change:
I suggest refactoring the class to use the @property decorator for value. This would allow us to:
Enforce Read-Only Access: By providing a getter without a setter, we can prevent accidental overwrites.
Maintain Validation: If a setter is desired, we can ensure that any updates to the vector's data are subjected to the same ndim and dtype checks as the constructor.
Benefit:
This improves the robustness of the library by ensuring that a HalfVector instance always represents a valid vector, preventing downstream errors in serialization or database operations.
I’m happy to submit a Pull Request for this if the maintainers feel this aligns with the project's performance and design goals!