Skip to content

Commit

Permalink
model.base: fix ModelReference.resolve() error messages for element…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
jkhsjdhjs authored and s-heppner committed Oct 3, 2023
1 parent 8cdfcb7 commit 09b68a4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,13 +1017,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):
Expand Down
4 changes: 2 additions & 2 deletions test/model/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 09b68a4

Please sign in to comment.