Skip to content

Commit cd39f2e

Browse files
authored
fix: NamespaceSet.pop() removes item from all backends (#514)
pop() only removed item from first backend via popitem(), leaving stale entries in remaining backends and causing false AASConstraintViolation on subsequent add() for same semantic_id. Fixes #496
1 parent 855693e commit cd39f2e

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

sdk/basyx/aas/model/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,8 +2077,10 @@ def discard(self, x: _NSO) -> None:
20772077

20782078
def pop(self) -> _NSO:
20792079
_, value = next(iter(self._backend.values()))[0].popitem()
2080+
for key_attr_name, (backend_dict, case_sensitive) in self._backend.items():
2081+
key_attr_value = self._get_attribute(value, key_attr_name, case_sensitive)
2082+
backend_dict.pop(key_attr_value, None)
20802083
self._execute_item_del_hook(value)
2081-
value.parent = None
20822084
return value
20832085

20842086
def clear(self) -> None:

sdk/test/model/test_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ def setUp(self):
338338
self.namespace = self._namespace_class()
339339
self.namespace3 = self._namespace_class_qualifier()
340340

341+
def test_namespaceset_pop_removes_from_all_backends(self) -> None:
342+
# set1 has two backends: id_short and semantic_id
343+
self.namespace.set1.add(self.prop1)
344+
popped = self.namespace.set1.pop()
345+
self.assertIs(self.prop1, popped)
346+
self.assertEqual(0, len(self.namespace.set1))
347+
# After pop, adding a new item with the same semantic_id must NOT raise AASConstraintViolation —
348+
# it would if the popped item's semantic_id entry were still in the backend
349+
new_prop = model.Property("NewProp", model.datatypes.Int, semantic_id=self.propSemanticID)
350+
self.namespace.set1.add(new_prop)
351+
self.assertEqual(1, len(self.namespace.set1))
352+
341353
def test_NamespaceSet(self) -> None:
342354
self.namespace.set1.add(self.prop1)
343355
self.assertEqual(1, len(self.namespace.set1))

0 commit comments

Comments
 (0)