Skip to content

Commit

Permalink
Equality comparisons return false when used with the wrong class.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Jan 19, 2024
1 parent da758a5 commit 5b9deb3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/biocutils/Factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,21 @@ def __str__(self) -> str:
message += "ordered: " + str(self._ordered)
return message

def __eq__(self, other: "Factor"):
"""
Args:
other: Another ``Factor``.
Returns:
Whether the current object is equal to ``other``, i.e.,
same codes, levels, names and ordered status.
"""
if not isinstance(other, Factor):
return False
if len(self) != len(other) or self._levels != other._levels or self._names != other._names or self._ordered != other._ordered:
return False
return (self._codes == other._codes).all()

###########################
#####>>>> Slicing <<<<#####
###########################
Expand Down
2 changes: 2 additions & 0 deletions src/biocutils/NamedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def __eq__(self, other: "NamedList") -> bool:
Whether the current object is equal to ``other``, i.e.,
same data and names.
"""
if not isinstance(other, NamedList):
return False
return self._data == other._data and self._names == other._names

#################################
Expand Down
2 changes: 2 additions & 0 deletions src/biocutils/Names.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def __eq__(self, other: "Names") -> bool:
Returns:
Whether the current object is the same as ``other``.
"""
if not isinstance(other, Names):
return False
return self._names == other._names

def as_list(self) -> List[str]:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_Factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def test_factor_iter():
assert output == ["A", "B", "C", None, "C", "E"]


def test_factor_comparisons():
f = Factor([0, 1, 2, 0, 2, 4], levels=["A", "B", "C", "D", "E"])
assert f == f
assert f != []
f2 = f.set_levels(["E", "C", "D", "B", "A"])
assert f != f2
f2 = f.set_ordered(True)
assert f != f2
f2 = Factor([0, 1, 2, 3, 4], levels=["A", "B", "C", "D", "E"])
assert f != f2
f2 = Factor([0, 1, 2, 3, 4, 0], levels=["A", "B", "C", "D", "E"])
assert f != f2


def test_Factor_print():
f = Factor([0, 1, 2, 0, 2, 4], levels=["A", "B", "C", "D", "E"])
assert repr(f).startswith("Factor(")
Expand Down
12 changes: 12 additions & 0 deletions tests/test_NamedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ def test_NamedList_addition():
assert x1.get_names().as_list() == ["A", "B", "C", "D", "E", "F", "G"]


def test_NamedList_comparison():
x1 = NamedList([1,2,3,4], names=["A", "B", "C", "D"])
assert x1 == x1
assert x1 != []
x2 = x1.set_names(None)
assert x2 != x1
x2 = NamedList([4,3,2,1], names=["A", "B", "C", "D"])
assert x2 != x1
x2 = NamedList([1,2,3,4], names=["a", "b", "c", "d"])
assert x2 != x1


def test_NamedList_copy():
x = NamedList([1,2,3,4])
y = x.copy()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_Names.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def test_Names_addition():
assert x1.as_list() == ["1", "2", "3", "4", "5", "6", "7"]


def test_Names_comparison():
x1 = Names([1,2,3,4])
assert x1 == x1
assert x1 != []
x2 = Names([4,3,2,1])
assert x2 != x1


def test_Names_generics():
x = Names([1,2,3,4])
sub = biocutils.subset_sequence(x, [0,3,2,1])
Expand Down

0 comments on commit 5b9deb3

Please sign in to comment.