From 917a35db270e05338c43022dc10d44076ba7467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Tue, 29 Aug 2023 15:46:53 +0200 Subject: [PATCH] model.base: make extending a ConstrainedList atomic Previously partial additions of elements via a call to `.extend()` were possible, since the `.extend()` method of the superclass simply calls `.append()` for each item, which in turn calls `.insert()`. Thus, if the hook call for one item raised an exception, all previous items had already been added. --- basyx/aas/model/base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/basyx/aas/model/base.py b/basyx/aas/model/base.py index a21185a0b..373b5459b 100644 --- a/basyx/aas/model/base.py +++ b/basyx/aas/model/base.py @@ -1223,6 +1223,16 @@ 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: + to_add: List[_T] = [] + for v in v_list: + self._item_add_hook(v, self._list + to_add) + to_add.append(v) + for v in v_list: + self._list.append(v) + @overload def __getitem__(self, index: int) -> _T: ...