diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index 09fe6e03..b753d3aa 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -133,7 +133,9 @@ def __getitem__( self, index: Union[int, str] ) -> Union[ODArray, ODRecord, ODVariable]: """Get object from object dictionary by name or index.""" - item = self.names.get(index) or self.indices.get(index) + item = self.names.get(index) + if item is None: + item = self.indices.get(index) if item is None: if isinstance(index, str) and '.' in index: idx, sub = index.split('.', maxsplit=1) diff --git a/test/test_od.py b/test/test_od.py index 52de86f8..9ab0e187 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -249,6 +249,18 @@ def test_get_item_dot(self): self.assertEqual(test_od["Test Array.Test Variable"], member1) self.assertEqual(test_od["Test Array.Test Variable 2"], member2) + def test_get_item_index(self): + test_od = od.ObjectDictionary() + array = od.ODArray("Test Array", 0x1000) + test_od.add_object(array) + item = test_od[0x1000] + self.assertIsInstance(item, od.ODArray) + self.assertIs(item, array) + item = test_od["Test Array"] + self.assertIsInstance(item, od.ODArray) + self.assertIs(item, array) + + class TestArray(unittest.TestCase): def test_subindexes(self):