Skip to content

Commit ba43d0c

Browse files
committed
Updated repr form to match python convention and added repr tests
1 parent 8b46ecf commit ba43d0c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

bson/binary.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin
248248
self.padding = padding
249249

250250
def __repr__(self) -> str:
251-
return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})"
251+
return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032, RUF100
252+
self.dtype, self.padding, self.data
253+
)
252254

253255

254256
class Binary(bytes):

test/test_bson.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,30 @@ def test_vector(self):
809809
dtype=BinaryVectorDtype.PACKED_BIT,
810810
) # type: ignore[call-overload]
811811

812+
def test_binaryvector_repr(self):
813+
"""Tests of repr(BinaryVector)"""
814+
data = [127, 7]
815+
zero = BinaryVector([], BinaryVectorDtype.INT8)
816+
self.assertEqual(
817+
repr(zero), "BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data=[])"
818+
)
819+
one = BinaryVector(data, BinaryVectorDtype.INT8)
820+
self.assertEqual(
821+
repr(one), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})"
822+
)
823+
two = BinaryVector(data, BinaryVectorDtype.FLOAT32)
824+
self.assertEqual(
825+
repr(two), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})"
826+
)
827+
three = BinaryVector(data, BinaryVectorDtype.FLOAT32, padding=0)
828+
self.assertEqual(
829+
repr(three), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})"
830+
)
831+
four = BinaryVector(data, BinaryVectorDtype.PACKED_BIT, padding=3)
832+
self.assertEqual(
833+
repr(four), f"BinaryVector(dtype=BinaryVectorDtype.PACKED_BIT, padding=3, data={data})"
834+
)
835+
812836
def test_unicode_regex(self):
813837
"""Tests we do not get a segfault for C extension on unicode RegExs.
814838
This had been happening.

0 commit comments

Comments
 (0)