Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.base: make extending a ConstrainedList atomic #116

Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,13 @@ def insert(self, index: int, value: _T) -> None:
self._item_add_hook(value, self._list)
self._list.insert(index, value)

def extend(self, values: Iterable[_T]) -> None:
v_list = list(values)
if self._item_add_hook is not None:
for idx, v in enumerate(v_list):
self._item_add_hook(v, self._list + v_list[:idx])
self._list = self._list + v_list

@overload
def __getitem__(self, index: int) -> _T: ...

Expand Down
24 changes: 24 additions & 0 deletions test/model/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,30 @@ def del_hook(itm: int, list_: List[int]) -> None:
self.assertEqual(existing_items, [1, 2, 3, 4, 10, 11])
check_list.pop()
self.assertEqual(c_list, check_list)
def test_atomicity(self) -> None:
s-heppner marked this conversation as resolved.
Show resolved Hide resolved
def hook(itm: int, _list: List[int]) -> None:
if itm > 2:
raise ValueError

c_list: model.ConstrainedList[int] = model.ConstrainedList([], item_add_hook=hook)
with self.assertRaises(ValueError):
c_list = model.ConstrainedList([1, 2, 3], item_add_hook=hook)
self.assertEqual(c_list, [])
with self.assertRaises(ValueError):
c_list.extend([1, 2, 3])
self.assertEqual(c_list, [])
c_list.extend([1, 2])
self.assertEqual(c_list, [1, 2])

c_list = model.ConstrainedList([1, 2, 3], item_del_hook=hook)
with self.assertRaises(ValueError):
del c_list[0:3]
self.assertEqual(c_list, [1, 2, 3])
with self.assertRaises(ValueError):
c_list.clear()
self.assertEqual(c_list, [1, 2, 3])
del c_list[0:2]
self.assertEqual(c_list, [3])


class LangStringSetTest(unittest.TestCase):
Expand Down
Loading