Skip to content

PYTHON-5289 Validate ignored bits are 0 on write for bson.BinaryVector #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

caseyclements
Copy link
Contributor

This is the implementation of the spec changes applied in specifications/pull/1812.

@caseyclements
Copy link
Contributor Author

Failing tests are unrelated.

bson/binary.py Outdated
@@ -522,6 +530,12 @@ def as_vector(self) -> BinaryVector:
dtype_format = "B"
format_string = f"<{n_values}{dtype_format}"
unpacked_uint8s = list(struct.unpack_from(format_string, self, position))
if padding and n_values and unpacked_uint8s[-1] & (1 << padding) - 1 != 0:
warnings.warn(
"Vector has a padding P, but bits in the final byte lower than P are non-zero. In the next major version, they must be zero.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the next major version, this warning will become an error to match the behavior in from_vector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We make the significant change (exception not warning) in the next major change and document in changelog and api. The ticket for that is PYTHON-5280 and has 5.0 as its fix version.

Copy link
Member

@ShaneHarvey ShaneHarvey Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add "pymongo version 5.0" rather than "the next major version". This helps show the user the version (5.0) and that the warning is coming from pymongo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure can.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any docs or changelog we need to update here?

except Exception as exc:
self.assertIsInstance(exc, ValueError)
else:
self.fail("Failed to raise an exception.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion should use assertRaises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. You got me. This looked funny when I reviewed it last week. I'll update to follow best practice.

bson/binary.py Outdated
@@ -522,6 +530,12 @@ def as_vector(self) -> BinaryVector:
dtype_format = "B"
format_string = f"<{n_values}{dtype_format}"
unpacked_uint8s = list(struct.unpack_from(format_string, self, position))
if padding and n_values and unpacked_uint8s[-1] & (1 << padding) - 1 != 0:
warnings.warn(
"Vector has a padding P, but bits in the final byte lower than P are non-zero. In the next major version, they must be zero.",
Copy link
Member

@ShaneHarvey ShaneHarvey Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add "pymongo version 5.0" rather than "the next major version". This helps show the user the version (5.0) and that the warning is coming from pymongo.

@caseyclements caseyclements requested a review from a team as a code owner July 1, 2025 21:56
@caseyclements caseyclements requested a review from ShaneHarvey July 1, 2025 23:25
@caseyclements
Copy link
Contributor Author

@ShaneHarvey I've made the changes that you recommended. What do you think?

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding a versionchanged entry to from_vector docstring.

@@ -51,6 +51,9 @@ PyMongo 4.13 brings a number of changes including:
or the `migration guide <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/reference/migration/>`_ for more information.
- Fixed a bug where :class:`pymongo.write_concern.WriteConcern` repr was not eval-able
when using ``w="majority"``.
- Ignored bits in a BSON BinaryVector of PACKED_BIT dtype should be set to zero.
On writes, this is enforced and is a breaking change.
Reads from the database will not fail, however a warning will be triggered.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest referring to BSON encoding vs decoding instead of writes vs reads.

@@ -51,6 +51,9 @@ PyMongo 4.13 brings a number of changes including:
or the `migration guide <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/reference/migration/>`_ for more information.
- Fixed a bug where :class:`pymongo.write_concern.WriteConcern` repr was not eval-able
when using ``w="majority"``.
- Ignored bits in a BSON BinaryVector of PACKED_BIT dtype should be set to zero.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Ignored bits" refers to the padding bits right? Should we say "padding" to avoid introducing a new term?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants