Skip to content

Commit ceb6786

Browse files
committed
BinaryVector is no longer a dataclass. Instead, we implement __eq__
1 parent 023d8ca commit ceb6786

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

bson/binary.py

Lines changed: 7 additions & 2 deletions
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
@@ -250,6 +248,13 @@ def __init__(self, data: Sequence[float | int], dtype: BinaryVectorDtype, paddin
250248
def __repr__(self) -> str:
251249
return f"BinaryVector(dtype={self.dtype}, padding={self.padding}, data={self.data})"
252250

251+
def __eq__(self, other: BinaryVector) -> 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+
253258

254259
class Binary(bytes):
255260
"""Representation of BSON binary data.

test/test_bson.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,24 @@ def test_binaryvector_repr(self):
849849
)
850850
self.assertRepr(zero)
851851

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+
852870
def test_unicode_regex(self):
853871
"""Tests we do not get a segfault for C extension on unicode RegExs.
854872
This had been happening.

0 commit comments

Comments
 (0)