From e31fbe36268c5e5e34da57c5a97083e21404b99d Mon Sep 17 00:00:00 2001 From: Afloat <132809511+Afloat16@users.noreply.github.com> Date: Thu, 15 Oct 2026 18:09:15 +0800 Subject: [PATCH 1/2] fix(urdf): initialise name maps before transmission validation --- src/roboticstoolbox/tools/urdf/urdf.py | 23 ++++++----- tests/test_urdf_name_maps.py | 54 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 tests/test_urdf_name_maps.py diff --git a/src/roboticstoolbox/tools/urdf/urdf.py b/src/roboticstoolbox/tools/urdf/urdf.py index 7eb4a329d..f41eb07ed 100644 --- a/src/roboticstoolbox/tools/urdf/urdf.py +++ b/src/roboticstoolbox/tools/urdf/urdf.py @@ -1855,13 +1855,6 @@ def __init__( if materials is None: materials = [] - # TODO, what does this next line do? - # why arent the other things validated - try: - self._validate_transmissions() - except Exception: - pass - self.name = name self.other_xml = other_xml @@ -1891,6 +1884,13 @@ def __init__( stage="urdf", ) + self._link_map = {link.name: link for link in self._links} + self._joint_map = {joint.name: joint for joint in self._joints} + self._transmission_map = { + transmission.name: transmission for transmission in self._transmissions + } + self._validate_transmissions() + @property def name(self) -> Any: """str : The name of the URDF.""" @@ -1916,8 +1916,7 @@ def link_map(self) -> Any: # pragma nocover directly. If you want to add or remove links, use the appropriate functions. """ - # `_link_map` is never set: AttributeError, see findings - return copy.copy(self._link_map) # type: ignore[attr-defined] + return copy.copy(self._link_map) @property def joints(self) -> Any: @@ -1935,7 +1934,7 @@ def joint_map(self) -> Any: # pragma nocover directly. If you want to add or remove joints, use the appropriate functions. """ - return copy.copy(self._joint_map) # type: ignore[attr-defined] + return copy.copy(self._joint_map) @property def transmissions(self) -> Any: @@ -1953,7 +1952,7 @@ def transmission_map(self) -> Any: # pragma nocover directly. If you want to add or remove transmissions, use the appropriate functions. """ - return copy.copy(self._transmission_map) # type: ignore[attr-defined] + return copy.copy(self._transmission_map) @property def other_xml(self) -> Any: # pragma nocover @@ -2058,7 +2057,7 @@ def _validate_transmissions(self) -> None: """ for t in self.transmissions: # pragma nocover for joint in t.joints: - if joint.name not in self._joint_map: # type: ignore[attr-defined] + if joint.name not in self._joint_map: raise ValueError( f"Transmission {t.name} has invalid joint name {joint.name}" ) diff --git a/tests/test_urdf_name_maps.py b/tests/test_urdf_name_maps.py new file mode 100644 index 000000000..d3a838e0d --- /dev/null +++ b/tests/test_urdf_name_maps.py @@ -0,0 +1,54 @@ +"""Parsed URDF names must be available to callers and transmission validation.""" + +import pytest + +from roboticstoolbox.tools.urdf import URDF, URDFError + +ROBOT = """ + + + + + + + transmission_interface/SimpleTransmission + + PositionJointInterface + + + PositionJointInterface + 2 + + +""" + + +def test_name_maps_preserve_parsed_object_identity_and_return_copies(): + robot = URDF.loadstr(ROBOT.format(joint="hinge"), None) + + for mapping_name, sequence_name in ( + ("link_map", "links"), + ("joint_map", "joints"), + ("transmission_map", "transmissions"), + ): + mapping = getattr(robot, mapping_name) + objects = getattr(robot, sequence_name) + assert set(mapping) == {item.name for item in objects} + for item in objects: + assert mapping[item.name] is item + mapping.clear() + assert len(getattr(robot, mapping_name)) == len(objects) + + +def test_no_joints_or_transmissions_have_empty_name_maps(): + robot = URDF.loadstr('', None) + assert robot.link_map["base"] is robot.links[0] + assert robot.joint_map == {} + assert robot.transmission_map == {} + + +def test_transmission_must_reference_an_existing_joint(): + with pytest.raises( + URDFError, match="Transmission drive has invalid joint name absent" + ): + URDF.loadstr(ROBOT.format(joint="absent"), None) From 0cde7625059e84ce3434012730d8714de43506b7 Mon Sep 17 00:00:00 2001 From: Afloat <132809511+Afloat16@users.noreply.github.com> Date: Thu, 15 Oct 2026 18:39:39 +0800 Subject: [PATCH 2/2] test(urdf): retain name-map checks in optimised Python --- tests/test_urdf_name_maps.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test_urdf_name_maps.py b/tests/test_urdf_name_maps.py index d3a838e0d..e1704228c 100644 --- a/tests/test_urdf_name_maps.py +++ b/tests/test_urdf_name_maps.py @@ -1,5 +1,6 @@ """Parsed URDF names must be available to callers and transmission validation.""" +import numpy.testing as nt import pytest from roboticstoolbox.tools.urdf import URDF, URDFError @@ -33,18 +34,18 @@ def test_name_maps_preserve_parsed_object_identity_and_return_copies(): ): mapping = getattr(robot, mapping_name) objects = getattr(robot, sequence_name) - assert set(mapping) == {item.name for item in objects} + nt.assert_equal(set(mapping) == {item.name for item in objects}, True) for item in objects: - assert mapping[item.name] is item + nt.assert_equal(mapping[item.name] is item, True) mapping.clear() - assert len(getattr(robot, mapping_name)) == len(objects) + nt.assert_equal(len(getattr(robot, mapping_name)), len(objects)) def test_no_joints_or_transmissions_have_empty_name_maps(): robot = URDF.loadstr('', None) - assert robot.link_map["base"] is robot.links[0] - assert robot.joint_map == {} - assert robot.transmission_map == {} + nt.assert_equal(robot.link_map["base"] is robot.links[0], True) + nt.assert_equal(robot.joint_map, {}) + nt.assert_equal(robot.transmission_map, {}) def test_transmission_must_reference_an_existing_joint():