Skip to content

Commit

Permalink
forbid parameter to be declared statically without initialization. (#…
Browse files Browse the repository at this point in the history
…1216)

* forbid parameter to be declared statically without initialization.

Signed-off-by: Tomoya Fujita <[email protected]>

* add test cases for declare_parameter.

Signed-off-by: Tomoya Fujita <[email protected]>

---------

Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya authored Feb 9, 2024
1 parent 9655aa4 commit 220d714
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions rclpy/rclpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,16 +499,16 @@ def declare_parameters(
descriptor.type = second_arg.value
else:
value = second_arg
if not descriptor.dynamic_typing and value is not None:
if not descriptor.dynamic_typing:
# infer type from default value
if not isinstance(value, ParameterValue):
descriptor.type = Parameter.Type.from_parameter_value(value).value
else:
if value.type == ParameterType.PARAMETER_NOT_SET:
raise ValueError(
'Cannot declare a statically typed parameter with default value '
'of type PARAMETER_NOT_SET')
if isinstance(value, ParameterValue):
descriptor.type = value.type
else:
descriptor.type = Parameter.Type.from_parameter_value(value).value
if descriptor.type == ParameterType.PARAMETER_NOT_SET:
raise ValueError(
'Cannot declare a statically typed parameter with default value '
'of type PARAMETER_NOT_SET')

# Get value from parameter overrides, of from tuple if it doesn't exist.
if not ignore_override and name in self._parameter_overrides:
Expand Down
10 changes: 10 additions & 0 deletions rclpy/test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,16 @@ def test_declare_parameter(self):
self.node.declare_parameter(
'wrong_parameter_descriptor_type', 1, ParameterValue())

with self.assertRaises(ValueError):
self.node.declare_parameter(
'static_typing_wo_default_value',
descriptor=ParameterDescriptor(type=ParameterType.PARAMETER_NOT_SET))

with self.assertRaises(ValueError):
self.node.declare_parameter(
'static_typing_wo_default_value',
descriptor=ParameterDescriptor(type=ParameterType.PARAMETER_STRING))

with self.assertRaises(ValueError):
self.node.declare_parameter(
'dynamic_typing_and_static_type',
Expand Down

0 comments on commit 220d714

Please sign in to comment.