From b25401dec9eb9f82e91cd8adafbea8c85680cd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Fri, 29 Sep 2023 16:52:01 +0200 Subject: [PATCH] model.base: fix `ModelReference.resolve()` error messages for elements contained in `SubmodelElementLists` Some error messages raised in `ModelReference.resolve()` make use of the `resolved_keys` local list variable, which keeps track of all Identifiers, id_shorts and `SubmodelElementList` indices that have been resolved successfully. Instead of `SubmodelElementList` indices, the children's id_shorts were added to this list previously, which is inconsistent, and soon would cease to work anyway, since AASd-120 prohibits specifying id_shorts for children of `SubmodelElementList`. This commit fixes this such that indices are added and adjusts the tests accordingly. --- basyx/aas/model/base.py | 3 ++- test/model/test_base.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/basyx/aas/model/base.py b/basyx/aas/model/base.py index bacd8b005..0a8b1d539 100644 --- a/basyx/aas/model/base.py +++ b/basyx/aas/model/base.py @@ -1012,13 +1012,14 @@ def resolve(self, provider_: "provider.AbstractObjectProvider") -> _RT: # the `is_submodel_element_list` branch, but mypy doesn't infer types based on isinstance checks # stored in boolean variables. item = item.value[int(key.value)] # type: ignore + resolved_keys[-1] += f"[{key.value}]" else: item = item.get_referable(key.value) + resolved_keys.append(item.id_short) except (KeyError, IndexError) as e: raise KeyError("Could not resolve {} {} at {}".format( "index" if is_submodel_element_list else "id_short", key.value, " / ".join(resolved_keys)))\ from e - resolved_keys.append(item.id_short) # Check type if not isinstance(item, self.type): diff --git a/test/model/test_base.py b/test/model/test_base.py index cde414573..7e186db09 100644 --- a/test/model/test_base.py +++ b/test/model/test_base.py @@ -832,7 +832,7 @@ def get_identifiable(self, identifier: Identifier) -> Identifiable: model.Property) with self.assertRaises(TypeError) as cm_3: ref4.resolve(DummyObjectProvider()) - self.assertEqual("Object retrieved at urn:x-test:submodel / list / collection / prop is not a Namespace", + self.assertEqual("Object retrieved at urn:x-test:submodel / list[0] / prop is not a Namespace", str(cm_3.exception)) with self.assertRaises(AttributeError) as cm_4: @@ -863,7 +863,7 @@ def get_identifiable(self, identifier: Identifier) -> Identifiable: with self.assertRaises(KeyError) as cm_8: ref8.resolve(DummyObjectProvider()) - self.assertEqual("'Could not resolve id_short prop_false at urn:x-test:submodel / list / collection'", + self.assertEqual("'Could not resolve id_short prop_false at urn:x-test:submodel / list[0]'", str(cm_8.exception)) with self.assertRaises(ValueError) as cm_9: