Skip to content

Commit 4e74f5a

Browse files
Michael Jeronimojacobperron
andauthored
backport PR: [Foxy] Handle any substitution types for SetParameter name argument (#182) (#218)
* Handle any substitution types for SetParameter name argument (#182) * Handle any substitution types for SetParameter name argument This fixes an error when trying to use the SetParameter action in a front-end launch file. The parsed 'name' attribute is returned a list of substitutions, which cannot be used as a key in a dictionary. Signed-off-by: Jacob Perron <[email protected]> * Fix SetParameter type annotations Signed-off-by: Jacob Perron <[email protected]> * Remove test case that is invalid for foxy Signed-off-by: Michael Jeronimo <[email protected]> Co-authored-by: Jacob Perron <[email protected]>
1 parent 7013bc3 commit 4e74f5a

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

launch_ros/launch_ros/actions/set_parameter.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
"""Module for the `SetParameter` action."""
1616

17-
from typing import List
18-
1917
from launch import Action
20-
from launch import Substitution
2118
from launch.frontend import Entity
2219
from launch.frontend import expose_action
2320
from launch.frontend import Parser
2421
from launch.launch_context import LaunchContext
2522
from launch.some_substitutions_type import SomeSubstitutionsType
23+
from launch.utilities import normalize_to_list_of_substitutions
2624

25+
from launch_ros.parameters_type import ParameterName
26+
from launch_ros.parameters_type import ParameterValue
2727
from launch_ros.parameters_type import SomeParameterValue
2828
from launch_ros.utilities.evaluate_parameters import evaluate_parameter_dict
2929
from launch_ros.utilities.normalize_parameters import normalize_parameter_dict
@@ -62,7 +62,8 @@ def __init__(
6262
) -> None:
6363
"""Create a SetParameter action."""
6464
super().__init__(**kwargs)
65-
self.__param_dict = normalize_parameter_dict({name: value})
65+
normalized_name = normalize_to_list_of_substitutions(name)
66+
self.__param_dict = normalize_parameter_dict({tuple(normalized_name): value})
6667

6768
@classmethod
6869
def parse(cls, entity: Entity, parser: Parser):
@@ -73,12 +74,12 @@ def parse(cls, entity: Entity, parser: Parser):
7374
return cls, kwargs
7475

7576
@property
76-
def name(self) -> List[Substitution]:
77+
def name(self) -> ParameterName:
7778
"""Getter for name."""
7879
return self.__param_dict.keys()[0]
7980

8081
@property
81-
def value(self) -> List[Substitution]:
82+
def value(self) -> ParameterValue:
8283
"""Getter for value."""
8384
return self.__param_dict.values()[0]
8485

test_launch_ros/test/test_launch_ros/actions/test_set_parameter.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from launch import LaunchContext
1818
from launch.actions import PopLaunchConfigurations
1919
from launch.actions import PushLaunchConfigurations
20+
from launch.substitutions import TextSubstitution
2021

2122
from launch_ros.actions import Node
2223
from launch_ros.actions import SetParameter
@@ -39,15 +40,30 @@ def perform_substitution(self, sub):
3940
def get_set_parameter_test_parameters():
4041
return [
4142
pytest.param(
42-
[{'my_param': '2'}], # to set
43+
[('my_param', '2')], # to set
4344
{'my_param': '2'}, # expected
4445
id='One param'
4546
),
4647
pytest.param(
47-
[{'my_param': '2', 'another_param': '2'}, {'my_param': '3'}],
48+
[('my_param', '2'), ('another_param', '2'), ('my_param', '3')],
4849
{'my_param': '3', 'another_param': '2'},
4950
id='Two params, overriding one'
5051
),
52+
pytest.param(
53+
[(TextSubstitution(text='my_param'), TextSubstitution(text='my_value'))],
54+
{'my_param': 'my_value'},
55+
id='Substitution types'
56+
),
57+
pytest.param(
58+
[((TextSubstitution(text='my_param'),), (TextSubstitution(text='my_value'),))],
59+
{'my_param': 'my_value'},
60+
id='Tuple of substitution types'
61+
),
62+
pytest.param(
63+
[([TextSubstitution(text='my_param')], [TextSubstitution(text='my_value')])],
64+
{'my_param': 'my_value'},
65+
id='List of substitution types'
66+
),
5167
]
5268

5369

@@ -57,9 +73,8 @@ def get_set_parameter_test_parameters():
5773
)
5874
def test_set_param(params_to_set, expected_result):
5975
set_parameter_actions = []
60-
for dicts in params_to_set:
61-
for name, value in dicts.items():
62-
set_parameter_actions.append(SetParameter(name=name, value=value))
76+
for name, value in params_to_set:
77+
set_parameter_actions.append(SetParameter(name=name, value=value))
6378
lc = MockContext()
6479
for set_param in set_parameter_actions:
6580
set_param.execute(lc)

0 commit comments

Comments
 (0)