diff --git a/rclpy/rclpy/node.py b/rclpy/rclpy/node.py index 425a7992d..425a468bb 100644 --- a/rclpy/rclpy/node.py +++ b/rclpy/rclpy/node.py @@ -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: diff --git a/rclpy/test/test_node.py b/rclpy/test/test_node.py index 4c4758596..ec441a384 100644 --- a/rclpy/test/test_node.py +++ b/rclpy/test/test_node.py @@ -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',