Skip to content

Commit 3cb77e9

Browse files
author
Scott Griffiths
committed
== and != being misused a lot when comparing Arrays. Concealing some bugs.
1 parent d9beb0d commit 3cb77e9

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

bitstring/array_.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,17 @@ def __le__(self, other: Union[int, float, Array]) -> Array:
722722
return self._apply_op_between_arrays(operator.le, other, is_comparison=True)
723723
return self._apply_op_to_all_elements(operator.le, other, is_comparison=True)
724724

725-
def __eq__(self, other: Union[int, float, str, BitsType, Array]) -> Array:
726-
if isinstance(other, Array):
727-
return self._apply_op_between_arrays(operator.eq, other, is_comparison=True)
728-
return self._apply_op_to_all_elements(operator.eq, other, is_comparison=True)
729-
730-
def __ne__(self, other: Union[int, float, str, BitsType, Array]) -> Array:
731-
if isinstance(other, Array):
732-
return self._apply_op_between_arrays(operator.ne, other, is_comparison=True)
733-
return self._apply_op_to_all_elements(operator.ne, other, is_comparison=True)
725+
def __eq__(self, other: Union[int, float, str, Array]) -> Array:
726+
if isinstance(other, (int, float, str, Bits)):
727+
return self._apply_op_to_all_elements(operator.eq, other, is_comparison=True)
728+
other = Array(self.dtype, other)
729+
return self._apply_op_between_arrays(operator.eq, other, is_comparison=True)
730+
731+
def __ne__(self, other: Union[int, float, str, Array]) -> Array:
732+
if isinstance(other, (int, float, str, Bits)):
733+
return self._apply_op_to_all_elements(operator.ne, other, is_comparison=True)
734+
other = Array(self.dtype, other)
735+
return self._apply_op_between_arrays(operator.ne, other, is_comparison=True)
734736

735737
# Unary operators
736738

tests/test_array.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_format_changes(self):
163163
b = a[:]
164164
b.dtype = 'int8'
165165
assert a.tolist() == b.tolist()
166-
assert a != b
166+
assert not a.equals(b)
167167
with pytest.raises(ValueError):
168168
b.dtype = 'hello_everyone'
169169
with pytest.raises(ValueError):
@@ -277,18 +277,18 @@ def test_equivalence(self):
277277
assert a.data == b.data
278278

279279
b = array.array('f', [54.2, -998, 411.9])
280-
assert a == b
280+
assert a.equals(b)
281281
a.dtype = 'bool'
282-
assert a != b
282+
assert not a.equals(b)
283283
a.dtype = 'floatne16'
284-
assert a != b
284+
assert not a.equals(b)
285285
a.dtype = 'floatne32'
286286
a.data += '0x0'
287-
assert a != b
287+
assert not a.equals(b)
288288
a.data += '0x0000000'
289-
assert a != b
289+
assert not a.equals(b)
290290
b.append(0.0)
291-
assert a == b
291+
assert a.equals(b)
292292

293293
def test_extend(self):
294294
a = Array('uint:3', (1, 2, 3))
@@ -320,7 +320,7 @@ def test_extend_with_mixed_classes(self):
320320

321321
a.dtype = 'int8'
322322
ap = Array('uint8', a.tolist())
323-
assert a != ap
323+
assert not a.equals(ap)
324324
assert a.tolist() == ap.tolist()
325325

326326
def test_insert(self):
@@ -814,9 +814,17 @@ def test_less_than_with_array(self):
814814
def test_array_equals(self):
815815
a = Array('i12', [1, 2, -3, 4, -5, 6])
816816
b = Array('i12', [6, 5, 4, 3, 2, 1])
817-
assert abs(a) == b[::-1]
818-
assert a != b
819-
817+
assert abs(a).equals(b[::-1])
818+
assert (a == b) == [False, False, False, False, False, False]
819+
assert (a != b) == [True, True, True, True, True, True]
820+
with pytest.raises(ValueError):
821+
_ = a == b[:-1]
822+
with pytest.raises(ValueError):
823+
_ = a == [1, 2, 3]
824+
with pytest.raises(ValueError):
825+
_ = [1, 2, 3] == a
826+
with pytest.raises(ValueError):
827+
_ = a == [1, 2, 3, 4, 5, 6, 7]
820828

821829
class TestAsType:
822830

0 commit comments

Comments
 (0)