Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions canopen/objectdictionary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __iter__(self) -> Iterator[int]:
def __len__(self) -> int:
return len(self.indices)

def __contains__(self, index: Union[int, str]):
def __contains__(self, index: object) -> bool:
return index in self.names or index in self.indices

def add_object(self, obj: Union[ODArray, ODRecord, ODVariable]) -> None:
Expand Down Expand Up @@ -234,10 +234,12 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[int]:
return iter(sorted(self.subindices))

def __contains__(self, subindex: Union[int, str]) -> bool:
def __contains__(self, subindex: object) -> bool:
return subindex in self.names or subindex in self.subindices

def __eq__(self, other: ODRecord) -> bool:
def __eq__(self, other: object) -> bool:
if not isinstance(other, ODRecord):
return NotImplemented
return self.index == other.index

def add_member(self, variable: ODVariable) -> None:
Expand Down Expand Up @@ -298,7 +300,9 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[int]:
return iter(sorted(self.subindices))

def __eq__(self, other: ODArray) -> bool:
def __eq__(self, other: object) -> bool:
if not isinstance(other, ODArray):
return NotImplemented
return self.index == other.index

def add_member(self, variable: ODVariable) -> None:
Expand Down Expand Up @@ -387,7 +391,9 @@ def qualname(self) -> str:
return f"{self.parent.name}.{self.name}"
return self.name

def __eq__(self, other: ODVariable) -> bool:
def __eq__(self, other: object) -> bool:
if not isinstance(other, ODVariable):
return NotImplemented
return (self.index == other.index and
self.subindex == other.subindex)

Expand Down
8 changes: 5 additions & 3 deletions canopen/sdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __iter__(self) -> Iterator[int]:
def __len__(self) -> int:
return len(self.od)

def __contains__(self, key: Union[int, str]) -> bool:
def __contains__(self, key: object) -> bool:
return key in self.od

def get_variable(
Expand Down Expand Up @@ -113,7 +113,7 @@ def __len__(self) -> int:
# Skip the "highest subindex" entry, which is not part of the data
return len(self.od) - int(0 in self.od)

def __contains__(self, subindex: Union[int, str]) -> bool:
def __contains__(self, subindex: object) -> bool:
return subindex in self.od


Expand All @@ -136,7 +136,9 @@ def __iter__(self) -> Iterator[int]:
def __len__(self) -> int:
return self[0].raw

def __contains__(self, subindex: int) -> bool:
def __contains__(self, subindex: object) -> bool:
if not isinstance(subindex, int):
return False
return 0 <= subindex <= len(self)


Expand Down
18 changes: 18 additions & 0 deletions test/test_od.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,23 @@ def test_subindexes(self):
self.assertEqual(array[3].name, "Test Variable_3")


class TestEquality(unittest.TestCase):

def test_record_eq_wrong_type(self):
record = od.ODRecord("Test Record", 0x1001)
self.assertNotEqual(record, "not a record")
self.assertNotEqual(record, 42)

def test_array_eq_wrong_type(self):
array = od.ODArray("Test Array", 0x1002)
self.assertNotEqual(array, "not an array")
self.assertNotEqual(array, 42)

def test_variable_eq_wrong_type(self):
var = od.ODVariable("Test Variable", 0x1000, 0)
self.assertNotEqual(var, "not a variable")
self.assertNotEqual(var, 42)


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions test/test_sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_array_members_dynamic(self):
for var in array.values():
self.assertIsInstance(var, canopen.sdo.SdoVariable)

def test_array_contains_non_int(self):
"""SdoArray.__contains__ should handle non-int types gracefully."""
array = self.sdo_node[0x1003]
self.assertNotIn("not an int", array)
self.assertNotIn(None, array)


class TestSDO(unittest.TestCase):
"""
Expand Down
Loading