Fix __contains__ and __eq__ override signatures by bizfsc · Pull Request #649 · canopen-python/canopen · GitHub
Skip to content
Merged
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
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
Loading