Skip to content

Commit 32bba4d

Browse files
committed
Update types of declare_parameter, declare_parameters api
1 parent bc7c46b commit 32bba4d

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

rclpy/rclpy/node.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from typing import List
2222
from typing import Optional
2323
from typing import overload
24-
from typing import Sequence
2524
from typing import Tuple
2625
from typing import Type
2726
from typing import TypeVar
@@ -164,7 +163,7 @@ def __init__(
164163
"""
165164
self.__handle = None
166165
self._context = get_default_context() if context is None else context
167-
self._parameters: dict = {}
166+
self._parameters: Dict[str, Parameter] = {}
168167
self._publishers: List[Publisher] = []
169168
self._subscriptions: List[Subscription] = []
170169
self._clients: List[Client] = []
@@ -179,8 +178,8 @@ def __init__(
179178
self._post_set_parameters_callbacks: List[Callable[[List[Parameter]], None]] = []
180179
self._rate_group = ReentrantCallbackGroup()
181180
self._allow_undeclared_parameters = allow_undeclared_parameters
182-
self._parameter_overrides: Dict[str, Parameter] = {}
183-
self._descriptors = {}
181+
self._parameter_overrides: Dict[str, Parameter[AllowableParameterValue]] = {}
182+
self._descriptors: Dict[str, ParameterDescriptor] = {}
184183

185184
namespace = namespace or ''
186185
if not self._context.ok():
@@ -357,20 +356,22 @@ def get_logger(self):
357356
return self._logger
358357

359358
@overload
360-
def declare_parameter(self, name: str, value: None = None,
359+
def declare_parameter(self, name: str,
360+
value: Union[None, Parameter.Type, ParameterValue] = None,
361361
descriptor: Optional[ParameterDescriptor] = None,
362362
ignore_override: bool = False) -> Parameter[None]: ...
363363

364364
@overload
365-
def declare_parameter(self, name: str, value: AllowableParameterValueT,
365+
def declare_parameter(self, name: str, value: Union[AllowableParameterValueT,
366+
Parameter.Type, ParameterValue],
366367
descriptor: Optional[ParameterDescriptor] = None,
367368
ignore_override: bool = False
368369
) -> Parameter[AllowableParameterValueT]: ...
369370

370371
def declare_parameter(
371372
self,
372373
name: str,
373-
value: AllowableParameterValue = None,
374+
value: Union[AllowableParameterValue, Parameter.Type, ParameterValue] = None,
374375
descriptor: Optional[ParameterDescriptor] = None,
375376
ignore_override: bool = False
376377
) -> Parameter:
@@ -395,7 +396,8 @@ def declare_parameter(
395396
"""
396397
if value is None and descriptor is None:
397398
# Temporal patch so we get deprecation warning if only a name is provided.
398-
args: Union[Tuple[str], Tuple[str, AllowableParameterValue,
399+
args: Union[Tuple[str], Tuple[str, Union[AllowableParameterValue,
400+
Parameter.Type, ParameterValue],
399401
ParameterDescriptor]] = (name, )
400402
else:
401403
descriptor = ParameterDescriptor() if descriptor is None else descriptor
@@ -408,7 +410,8 @@ def declare_parameters(
408410
parameters: List[Union[
409411
Tuple[str],
410412
Tuple[str, Parameter.Type],
411-
Tuple[str, AllowableParameterValue, ParameterDescriptor],
413+
Tuple[str, Union[AllowableParameterValue, Parameter.Type, ParameterValue],
414+
ParameterDescriptor],
412415
]],
413416
ignore_override: bool = False
414417
) -> List[Parameter]:
@@ -483,9 +486,8 @@ def declare_parameters(
483486
# Note(jubeira): declare_parameters verifies the name, but set_parameters doesn't.
484487
validate_parameter_name(name)
485488

486-
second_arg = parameter_tuple[1] if 1 < len(parameter_tuple) else None
487-
descriptor = parameter_tuple[2] if 2 < len(parameter_tuple) else ParameterDescriptor()
488-
489+
second_arg = parameter_tuple[1] if len(parameter_tuple) > 1 else None
490+
descriptor = parameter_tuple[2] if len(parameter_tuple) > 2 else ParameterDescriptor()
489491
if not isinstance(descriptor, ParameterDescriptor):
490492
raise TypeError(
491493
f'Third element {descriptor} at index {index} in parameters list '
@@ -529,6 +531,10 @@ def declare_parameters(
529531
if not ignore_override and name in self._parameter_overrides:
530532
value = self._parameter_overrides[name].value
531533

534+
if isinstance(value, ParameterValue):
535+
raise ValueError('Cannot declare a Parameter from a ParameterValue without it'
536+
'being included _parameter_overrides, and ignore_override=False')
537+
532538
parameter_list.append(Parameter(name, value=value))
533539
descriptors.update({name: descriptor})
534540

@@ -729,10 +735,7 @@ def get_parameter_or(
729735

730736
return self._parameters[name]
731737

732-
def get_parameters_by_prefix(self, prefix: str) -> Dict[str, Optional[Union[
733-
bool, int, float, str, bytes,
734-
Sequence[bool], Sequence[int], Sequence[float], Sequence[str]
735-
]]]:
738+
def get_parameters_by_prefix(self, prefix: str) -> Dict[str, Parameter]:
736739
"""
737740
Get parameters that have a given prefix in their names as a dictionary.
738741
@@ -1049,7 +1052,7 @@ def _check_undeclared_parameters(self, parameter_list: List[Parameter]):
10491052
if not self._allow_undeclared_parameters and any(undeclared_parameters):
10501053
raise ParameterNotDeclaredException(list(undeclared_parameters))
10511054

1052-
def _call_pre_set_parameters_callback(self, parameter_list: [List[Parameter]]):
1055+
def _call_pre_set_parameters_callback(self, parameter_list: List[Parameter]):
10531056
if self._pre_set_parameters_callbacks:
10541057
modified_parameter_list = []
10551058
for callback in self._pre_set_parameters_callbacks:
@@ -1059,7 +1062,7 @@ def _call_pre_set_parameters_callback(self, parameter_list: [List[Parameter]]):
10591062
else:
10601063
return None
10611064

1062-
def _call_post_set_parameters_callback(self, parameter_list: [List[Parameter]]):
1065+
def _call_post_set_parameters_callback(self, parameter_list: List[Parameter]):
10631066
if self._post_set_parameters_callbacks:
10641067
for callback in self._post_set_parameters_callbacks:
10651068
callback(parameter_list)

rclpy/rclpy/parameter.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import sys
1615
import array
16+
import sys
1717
from enum import IntEnum
1818
from typing import Dict
1919
from typing import Generic
@@ -41,7 +41,8 @@
4141
list[float], Tuple[float, ...], array.array[float],
4242
list[str], Tuple[str, ...], array.array[str]]
4343
AllowableParameterValueT = TypeVar('AllowableParameterValueT', None, bool, int, float, str,
44-
list[bytes], Tuple[bytes, ...], List[bool], Tuple[bool, ...],
44+
list[bytes], Tuple[bytes, ...], list[bool],
45+
Tuple[bool, ...],
4546
list[int], Tuple[int, ...], array.array[int],
4647
list[float], Tuple[float, ...], array.array[float],
4748
list[str], Tuple[str, ...], array.array[str])
@@ -54,10 +55,11 @@
5455
List[str], Tuple[str, ...], 'array.array[str]']
5556

5657
AllowableParameterValueT = TypeVar('AllowableParameterValueT', None, bool, int, float, str,
57-
List[bytes], Tuple[bytes, ...], List[bool], Tuple[bool, ...],
58-
List[int], Tuple[int, ...], 'array.array[int]',
59-
List[float], Tuple[float, ...], 'array.array[float]',
60-
List[str], Tuple[str, ...], 'array.array[str]')
58+
List[bytes], Tuple[bytes, ...], List[bool],
59+
Tuple[bool, ...], List[int], Tuple[int, ...],
60+
'array.array[int]', List[float], Tuple[float, ...],
61+
'array.array[float]', List[str], Tuple[str, ...],
62+
'array.array[str]')
6163

6264

6365
class Parameter(Generic[AllowableParameterValueT]):

0 commit comments

Comments
 (0)