Skip to content

Commit c41b2ca

Browse files
committed
fix: OrderedNamespaceSet.__setitem__ slice uses exhausted iterator
slice branch stored exhausted islice iterator back into _order instead of the materialized successful_new_items list, resulting in an empty slice assignment that discarded all newly added items. Fixes #494
1 parent 148bd85 commit c41b2ca

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

sdk/basyx/aas/model/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ def __setitem__(self, s, o) -> None:
22482248
for i in successful_new_items:
22492249
super().remove(i)
22502250
raise
2251-
self._order[s] = new_items
2251+
self._order[s] = successful_new_items
22522252
for i in deleted_items:
22532253
super().remove(i)
22542254

sdk/test/model/test_base.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,35 @@ def test_OrderedNamespace(self) -> None:
722722
f"{self._namespace_class.__name__}[{self.namespace.id}]'", # type: ignore[has-type]
723723
str(cm2.exception))
724724

725+
def test_ordered_namespaceset_slice_setitem_preserves_order(self) -> None:
726+
# Replace a slice of items; the new items must appear in the correct positions after replacement
727+
ns = ExampleOrderedNamespace()
728+
sid1 = model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, "http://example.org/sid1"),))
729+
sid2 = model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, "http://example.org/sid2"),))
730+
sid3 = model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, "http://example.org/sid3"),))
731+
sid4 = model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, "http://example.org/sid4"),))
732+
sid5 = model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, "http://example.org/sid5"),))
733+
p1 = model.Property("PA", model.datatypes.Int, semantic_id=sid1)
734+
p2 = model.Property("PB", model.datatypes.Int, semantic_id=sid2)
735+
p3 = model.Property("PC", model.datatypes.Int, semantic_id=sid3)
736+
ns.set1.add(p1)
737+
ns.set1.add(p2)
738+
ns.set1.add(p3)
739+
self.assertEqual([p1, p2, p3], list(ns.set1))
740+
741+
# Replace slice [0:2] (p1, p2) with two new items
742+
new1 = model.Property("PX", model.datatypes.Int, semantic_id=sid4)
743+
new2 = model.Property("PY", model.datatypes.Int, semantic_id=sid5)
744+
ns.set1[0:2] = [new1, new2]
745+
746+
# After replacement: [new1, new2, p3]
747+
result = list(ns.set1)
748+
self.assertEqual([new1, new2, p3], result)
749+
self.assertIsNone(p1.parent)
750+
self.assertIsNone(p2.parent)
751+
self.assertIs(ns, new1.parent)
752+
self.assertIs(ns, new2.parent)
753+
725754

726755
class ExternalReferenceTest(unittest.TestCase):
727756
def test_constraints(self):

0 commit comments

Comments
 (0)