-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5172 bugfix: Add __repr__ and __eq__ to bson.binary.BinaryVector #2162
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
Changes from 5 commits
519291c
a4ce5c7
8b46ecf
ba43d0c
b83bee4
8bd9482
389b1cf
916ec33
023d8ca
ceb6786
f35ef2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,6 +247,11 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin | |
self.dtype = dtype | ||
self.padding = padding | ||
|
||
def __repr__(self) -> str: | ||
return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032 | ||
self.dtype, self.padding, self.data | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hold up, the real bug here is that we're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that we had issues with the combo of {dataclass, slots, defaults}. So I've just removed the dataclass decorator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any other features that will break by removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. As mentioned in the closed PR, we've done init, and repr, and we don't want to rely on equality except via the binary representation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code seems happy to be a dataclass or not. I have no strong preference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you show an example of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. It's easy to try. Put a breakpoint on line 83 of test_bson_binary.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
>>> from bson.binary import *
>>> BinaryVector([1], BinaryVectorDtype.INT8) == BinaryVector([2], BinaryVectorDtype.INT8)
True So we need to remove dataclass and implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Take a look. I hate subtleties with == and floats.. |
||
|
||
class Binary(bytes): | ||
"""Representation of BSON binary data. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -809,6 +809,30 @@ def test_vector(self): | |
dtype=BinaryVectorDtype.PACKED_BIT, | ||
) # type: ignore[call-overload] | ||
|
||
def test_binaryvector_repr(self): | ||
"""Tests of repr(BinaryVector)""" | ||
data = [127, 7] | ||
zero = BinaryVector([], BinaryVectorDtype.INT8) | ||
self.assertEqual( | ||
repr(zero), "BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data=[])" | ||
) | ||
one = BinaryVector(data, BinaryVectorDtype.INT8) | ||
self.assertEqual( | ||
repr(one), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})" | ||
) | ||
two = BinaryVector(data, BinaryVectorDtype.FLOAT32) | ||
self.assertEqual( | ||
repr(two), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})" | ||
) | ||
three = BinaryVector(data, BinaryVectorDtype.FLOAT32, padding=0) | ||
self.assertEqual( | ||
repr(three), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})" | ||
) | ||
four = BinaryVector(data, BinaryVectorDtype.PACKED_BIT, padding=3) | ||
self.assertEqual( | ||
repr(four), f"BinaryVector(dtype=BinaryVectorDtype.PACKED_BIT, padding=3, data={data})" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add asserts using assertRepr like we do in test_connection_monitoring.py?: def assertRepr(self, obj):
new_obj = eval(repr(obj))
self.assertEqual(type(new_obj), type(obj))
self.assertEqual(repr(new_obj), repr(obj)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. That's very cool, that one can create an object using eval(repr(obj)) |
||
|
||
def test_unicode_regex(self): | ||
"""Tests we do not get a segfault for C extension on unicode RegExs. | ||
This had been happening. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f-string vs format makes no difference. If anything f-string will be faster.
__repr__
only runs when repr() is called.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if one has logger.debug(vector), is it not true that str.format won't be called, but the f-string will be created?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging behavior is unrelated to this PR since we're just adding a new method. The behavior of
repr(vector)
is the same whether we use f-string vs format() so let's go back to using f-string.