Skip to content

Commit

Permalink
model.base: make extending a ConstrainedList atomic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkhsjdhjs committed Aug 29, 2023
1 parent 07ca8d7 commit 917a35d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand Down

0 comments on commit 917a35d

Please sign in to comment.