From 519291c4f70d4b40f3f7935a796ee40470a93294 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Thu, 27 Feb 2025 13:50:17 -0500 Subject: [PATCH 01/11] Adds __repr__ to BinaryVector dataclass --- bson/binary.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bson/binary.py b/bson/binary.py index aab59cccbc..2becbef5f1 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -247,6 +247,9 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.dtype = dtype self.padding = padding + def __repr__(self): + return f"BinaryVector - {self.dtype=}, {self.dtype},{self.padding=}, {self.data=}" + class Binary(bytes): """Representation of BSON binary data. From a4ce5c773888f7462ba72962f47e99d162844e48 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Thu, 27 Feb 2025 14:04:00 -0500 Subject: [PATCH 02/11] Typing --- bson/binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/binary.py b/bson/binary.py index 2becbef5f1..7f6985b4da 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -247,7 +247,7 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.dtype = dtype self.padding = padding - def __repr__(self): + def __repr__(self) -> str: return f"BinaryVector - {self.dtype=}, {self.dtype},{self.padding=}, {self.data=}" From 8b46ecf1cb737c23135d0a26d9c8c0a3d99498a0 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Fri, 28 Feb 2025 09:33:45 -0500 Subject: [PATCH 03/11] Updated repr form to match python convention --- bson/binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/binary.py b/bson/binary.py index 7f6985b4da..96454135cb 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -248,7 +248,7 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.padding = padding def __repr__(self) -> str: - return f"BinaryVector - {self.dtype=}, {self.dtype},{self.padding=}, {self.data=}" + return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})" class Binary(bytes): From ba43d0c7f67ed4e2e52ea9e8d90e0ae90effcf30 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Fri, 28 Feb 2025 10:49:22 -0500 Subject: [PATCH 04/11] Updated repr form to match python convention and added repr tests --- bson/binary.py | 4 +++- test/test_bson.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/bson/binary.py b/bson/binary.py index 96454135cb..a43e6cdca9 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -248,7 +248,9 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.padding = padding def __repr__(self) -> str: - return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})" + return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032, RUF100 + self.dtype, self.padding, self.data + ) class Binary(bytes): diff --git a/test/test_bson.py b/test/test_bson.py index e704efe451..c15ba4732e 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -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})" + ) + def test_unicode_regex(self): """Tests we do not get a segfault for C extension on unicode RegExs. This had been happening. From b83bee4a9c84335e1387bb7480a387896399d2cb Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Fri, 28 Feb 2025 10:53:49 -0500 Subject: [PATCH 05/11] Removed unused noqa rule RUF100 --- bson/binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/binary.py b/bson/binary.py index a43e6cdca9..8e781d2cd2 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -248,7 +248,7 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.padding = padding def __repr__(self) -> str: - return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032, RUF100 + return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032 self.dtype, self.padding, self.data ) From 8bd9482215b7739b59340e80c7f19152888ce40c Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Fri, 28 Feb 2025 16:34:51 -0500 Subject: [PATCH 06/11] Reverted BinaryVector repr to f-string following python convention --- bson/binary.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bson/binary.py b/bson/binary.py index 8e781d2cd2..96454135cb 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -248,9 +248,7 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin self.padding = padding def __repr__(self) -> str: - return "BinaryVector(dtype={}, padding={}, data={})".format( # noqa: UP032 - self.dtype, self.padding, self.data - ) + return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})" class Binary(bytes): From 389b1cfb478b1ccb4e6d5036c21c62de0162b8f9 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Fri, 28 Feb 2025 17:34:27 -0500 Subject: [PATCH 07/11] Additional test of repr form: eval(repr(obj)) ~= obj --- test/test_bson.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/test/test_bson.py b/test/test_bson.py index c15ba4732e..4074e244db 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -809,29 +809,45 @@ def test_vector(self): dtype=BinaryVectorDtype.PACKED_BIT, ) # type: ignore[call-overload] + def assertRepr(self, obj): + new_obj = eval(repr(obj)) + self.assertEqual(type(new_obj), type(obj)) + self.assertEqual(repr(new_obj), repr(obj)) + 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) + + data = [1 / 127, -7 / 6] + one = BinaryVector(data, BinaryVectorDtype.FLOAT32) self.assertEqual( - repr(one), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})" + repr(one), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})" ) - two = BinaryVector(data, BinaryVectorDtype.FLOAT32) + self.assertRepr(one) + + data = [127, 7] + two = BinaryVector(data, BinaryVectorDtype.INT8) self.assertEqual( - repr(two), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})" + repr(two), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})" ) - three = BinaryVector(data, BinaryVectorDtype.FLOAT32, padding=0) + self.assertRepr(two) + + three = BinaryVector(data, BinaryVectorDtype.INT8, padding=0) self.assertEqual( - repr(three), f"BinaryVector(dtype=BinaryVectorDtype.FLOAT32, padding=0, data={data})" + repr(three), f"BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data={data})" ) + self.assertRepr(three) + four = BinaryVector(data, BinaryVectorDtype.PACKED_BIT, padding=3) self.assertEqual( repr(four), f"BinaryVector(dtype=BinaryVectorDtype.PACKED_BIT, padding=3, data={data})" ) + self.assertRepr(four) + + zero = BinaryVector([], BinaryVectorDtype.INT8) + self.assertEqual( + repr(zero), "BinaryVector(dtype=BinaryVectorDtype.INT8, padding=0, data=[])" + ) + self.assertRepr(zero) def test_unicode_regex(self): """Tests we do not get a segfault for C extension on unicode RegExs. From 916ec3329fe7b6bd244de25b0507542f308fd79a Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Mon, 3 Mar 2025 09:34:33 -0500 Subject: [PATCH 08/11] Remove dataclass decorator from BinaryVector. Instead, add __init__ and __repr__ manually. --- bson/binary.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bson/binary.py b/bson/binary.py index 96454135cb..26eb6d32bd 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -14,7 +14,6 @@ from __future__ import annotations import struct -from dataclasses import dataclass from enum import Enum from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload from uuid import UUID @@ -227,7 +226,6 @@ class BinaryVectorDtype(Enum): PACKED_BIT = b"\x10" -@dataclass class BinaryVector: """Vector of numbers along with metadata for binary interoperability. .. versionadded:: 4.10 From 023d8ca23ca9721961891dd46c8e725753f66ea2 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Wed, 5 Mar 2025 10:29:45 -0500 Subject: [PATCH 09/11] Revert "Remove dataclass decorator from BinaryVector. Instead, add __init__ and __repr__ manually." This reverts commit 916ec3329fe7b6bd244de25b0507542f308fd79a. --- bson/binary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bson/binary.py b/bson/binary.py index 26eb6d32bd..96454135cb 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -14,6 +14,7 @@ from __future__ import annotations import struct +from dataclasses import dataclass from enum import Enum from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload from uuid import UUID @@ -226,6 +227,7 @@ class BinaryVectorDtype(Enum): PACKED_BIT = b"\x10" +@dataclass class BinaryVector: """Vector of numbers along with metadata for binary interoperability. .. versionadded:: 4.10 From ceb67869617fb2a4d2fcbca68152954db3596ee4 Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Thu, 6 Mar 2025 11:09:28 -0500 Subject: [PATCH 10/11] BinaryVector is no longer a dataclass. Instead, we implement __eq__ --- bson/binary.py | 9 +++++++-- test/test_bson.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bson/binary.py b/bson/binary.py index 96454135cb..3ca56a80d1 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -14,7 +14,6 @@ from __future__ import annotations import struct -from dataclasses import dataclass from enum import Enum from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload from uuid import UUID @@ -227,7 +226,6 @@ class BinaryVectorDtype(Enum): PACKED_BIT = b"\x10" -@dataclass class BinaryVector: """Vector of numbers along with metadata for binary interoperability. .. versionadded:: 4.10 @@ -250,6 +248,13 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin def __repr__(self) -> str: return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})" + def __eq__(self, other: BinaryVector) -> bool: + if not isinstance(other, BinaryVector): + return False + return ( + self.dtype == other.dtype and self.padding == other.padding and self.data == other.data + ) + class Binary(bytes): """Representation of BSON binary data. diff --git a/test/test_bson.py b/test/test_bson.py index 4074e244db..6f26856b00 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -849,6 +849,24 @@ def test_binaryvector_repr(self): ) self.assertRepr(zero) + def test_binaryvector_equality(self): + """Tests of == __eq__""" + self.assertEqual( + BinaryVector([1.2, 1 - 1 / 3], BinaryVectorDtype.FLOAT32, 0), + BinaryVector([1.2, 1 - 1.0 / 3.0], BinaryVectorDtype.FLOAT32, 0), + ) + self.assertNotEqual( + BinaryVector([1.2, 1 - 1 / 3], BinaryVectorDtype.FLOAT32, 0), + BinaryVector([1.2, 6.0 / 9.0], BinaryVectorDtype.FLOAT32, 0), + ) + self.assertEqual( + BinaryVector([], BinaryVectorDtype.FLOAT32, 0), + BinaryVector([], BinaryVectorDtype.FLOAT32, 0), + ) + self.assertNotEqual( + BinaryVector([1], BinaryVectorDtype.INT8), BinaryVector([2], BinaryVectorDtype.INT8) + ) + def test_unicode_regex(self): """Tests we do not get a segfault for C extension on unicode RegExs. This had been happening. From f35ef2bf49657e86c7849f2906ca1e60d298f74f Mon Sep 17 00:00:00 2001 From: Casey Clements Date: Mon, 10 Mar 2025 14:47:53 -0400 Subject: [PATCH 11/11] Typing, other: Any --- bson/binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/binary.py b/bson/binary.py index 3ca56a80d1..eefc5697b6 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -248,7 +248,7 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin def __repr__(self) -> str: return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})" - def __eq__(self, other: BinaryVector) -> bool: + def __eq__(self, other: Any) -> bool: if not isinstance(other, BinaryVector): return False return (