Skip to content

Commit b66a5cb

Browse files
PYTHON-5172 bugfix: Add __repr__ and __eq__ to bson.binary.BinaryVector (#2162)
1 parent a548f7a commit b66a5cb

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

bson/binary.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from __future__ import annotations
1515

1616
import struct
17-
from dataclasses import dataclass
1817
from enum import Enum
1918
from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload
2019
from uuid import UUID
@@ -227,7 +226,6 @@ class BinaryVectorDtype(Enum):
227226
PACKED_BIT = b"\x10"
228227

229228

230-
@dataclass
231229
class BinaryVector:
232230
"""Vector of numbers along with metadata for binary interoperability.
233231
.. versionadded:: 4.10
@@ -247,6 +245,16 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin
247245
self.dtype = dtype
248246
self.padding = padding
249247

248+
def __repr__(self) -> str:
249+
return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})"
250+
251+
def __eq__(self, other: Any) -> bool:
252+
if not isinstance(other, BinaryVector):
253+
return False
254+
return (
255+
self.dtype == other.dtype and self.padding == other.padding and self.data == other.data
256+
)
257+
250258

251259
class Binary(bytes):
252260
"""Representation of BSON binary data.

test/test_bson.py

+58
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,64 @@ def test_vector(self):
809809
dtype=BinaryVectorDtype.PACKED_BIT,
810810
) # type: ignore[call-overload]
811811

812+
def assertRepr(self, obj):
813+
new_obj = eval(repr(obj))
814+
self.assertEqual(type(new_obj), type(obj))
815+
self.assertEqual(repr(new_obj), repr(obj))
816+
817+
def test_binaryvector_repr(self):
818+
"""Tests of repr(BinaryVector)"""
819+
820+
data = [1 / 127, -7 / 6]
821+
one = BinaryVector(data, BinaryVectorDtype.FLOAT32)
822+
self.assertEqual(
823+
repr(one), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})"
824+
)
825+
self.assertRepr(one)
826+
827+
data = [127, 7]
828+
two = BinaryVector(data, BinaryVectorDtype.INT8)
829+
self.assertEqual(
830+
repr(two), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})"
831+
)
832+
self.assertRepr(two)
833+
834+
three = BinaryVector(data, BinaryVectorDtype.INT8, padding=0)
835+
self.assertEqual(
836+
repr(three), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})"
837+
)
838+
self.assertRepr(three)
839+
840+
four = BinaryVector(data, BinaryVectorDtype.PACKED_BIT, padding=3)
841+
self.assertEqual(
842+
repr(four), f"BinaryVector(dtype=BinaryVectorDtype.PACKED_BIT, padding=3, data={data})"
843+
)
844+
self.assertRepr(four)
845+
846+
zero = BinaryVector([], BinaryVectorDtype.INT8)
847+
self.assertEqual(
848+
repr(zero), "BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data=[])"
849+
)
850+
self.assertRepr(zero)
851+
852+
def test_binaryvector_equality(self):
853+
"""Tests of == __eq__"""
854+
self.assertEqual(
855+
BinaryVector([1.2, 1 - 1 / 3], BinaryVectorDtype.FLOAT32, 0),
856+
BinaryVector([1.2, 1 - 1.0 / 3.0], BinaryVectorDtype.FLOAT32, 0),
857+
)
858+
self.assertNotEqual(
859+
BinaryVector([1.2, 1 - 1 / 3], BinaryVectorDtype.FLOAT32, 0),
860+
BinaryVector([1.2, 6.0 / 9.0], BinaryVectorDtype.FLOAT32, 0),
861+
)
862+
self.assertEqual(
863+
BinaryVector([], BinaryVectorDtype.FLOAT32, 0),
864+
BinaryVector([], BinaryVectorDtype.FLOAT32, 0),
865+
)
866+
self.assertNotEqual(
867+
BinaryVector([1], BinaryVectorDtype.INT8), BinaryVector([2], BinaryVectorDtype.INT8)
868+
)
869+
812870
def test_unicode_regex(self):
813871
"""Tests we do not get a segfault for C extension on unicode RegExs.
814872
This had been happening.

0 commit comments

Comments
 (0)