diff --git a/rclpy/rclpy/parameter_client.py b/rclpy/rclpy/parameter_client.py index e549a8698..c6482901e 100644 --- a/rclpy/rclpy/parameter_client.py +++ b/rclpy/rclpy/parameter_client.py @@ -32,6 +32,7 @@ from rclpy.qos import qos_profile_services_default from rclpy.qos import QoSProfile from rclpy.qos_overriding_options import QoSOverridingOptions +from rclpy.subscription import MessageInfo from rclpy.subscription import Subscription from rclpy.task import Future @@ -136,8 +137,8 @@ def list_parameters( self, prefixes: Optional[List[str]] = None, depth: Optional[int] = None, - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[ListParameters.Response], None]] = None + ) -> Future[ListParameters.Response]: """ List all parameters with given prefixes. @@ -156,7 +157,9 @@ def list_parameters( future.add_done_callback(callback) return future - def get_parameters(self, names: List[str], callback: Optional[Callable] = None) -> Future: + def get_parameters(self, names: List[str], + callback: Optional[Callable[[GetParameters.Response], None]] = None + ) -> Future[GetParameters.Response]: """ Get parameters given names. @@ -174,8 +177,8 @@ def get_parameters(self, names: List[str], callback: Optional[Callable] = None) def set_parameters( self, parameters: Sequence[Union[Parameter, ParameterMsg]], - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[SetParameters.Response], None]] = None + ) -> Future[SetParameters.Response]: """ Set parameters given a list of parameters. @@ -200,8 +203,8 @@ def set_parameters( def describe_parameters( self, names: List[str], - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[DescribeParameters.Response], None]] = None + ) -> Future[DescribeParameters.Response]: """ Describe parameters given names. @@ -222,8 +225,8 @@ def describe_parameters( def get_parameter_types( self, names: List[str], - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[GetParameterTypes.Response], None]] = None + ) -> Future[GetParameterTypes.Response]: """ Get parameter types given names. @@ -246,8 +249,8 @@ def get_parameter_types( def set_parameters_atomically( self, parameters: Sequence[Union[Parameter, ParameterMsg]], - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[SetParametersAtomically.Response], None]] = None + ) -> Future[SetParametersAtomically.Response]: """ Set parameters atomically. @@ -270,8 +273,9 @@ def set_parameters_atomically( return future def delete_parameters( - self, names: List[str], callback: Optional[Callable] = None - ) -> Future: + self, names: List[str], + callback: Optional[Callable[[SetParameters.Response], None]] = None + ) -> Future[SetParameters.Response]: """ Unset parameters with given names. @@ -295,8 +299,8 @@ def load_parameter_file( self, parameter_file: str, use_wildcard: bool = False, - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[SetParameters.Response], None]] = None + ) -> Future[SetParameters.Response]: """ Load parameters from a yaml file. @@ -317,8 +321,8 @@ def load_parameter_file_atomically( self, parameter_file: str, use_wildcard: bool = False, - callback: Optional[Callable] = None - ) -> Future: + callback: Optional[Callable[[SetParameters.Response], None]] = None + ) -> Future[SetParameters.Response]: """ Load parameters from a yaml file atomically. @@ -336,14 +340,15 @@ def load_parameter_file_atomically( return future def on_parameter_event( - self, callback: Callable, + self, callback: Union[Callable[[ParameterEvent], None], + Callable[[ParameterEvent, MessageInfo], None]], qos_profile: QoSProfile = qos_profile_parameter_events, *, callback_group: Optional[CallbackGroup] = None, event_callbacks: Optional[SubscriptionEventCallbacks] = None, qos_overriding_options: Optional[QoSOverridingOptions] = None, raw: bool = False - ) -> Subscription: + ) -> Subscription[ParameterEvent]: return self.node.create_subscription( ParameterEvent, '/parameter_events', diff --git a/rclpy/test/test_parameter_client.py b/rclpy/test/test_parameter_client.py index 6797a0f4a..6be3369b7 100644 --- a/rclpy/test/test_parameter_client.py +++ b/rclpy/test/test_parameter_client.py @@ -17,6 +17,7 @@ import unittest import rcl_interfaces.msg +from rcl_interfaces.msg import ParameterEvent from rcl_interfaces.msg import ParameterType import rcl_interfaces.srv import rclpy @@ -28,7 +29,7 @@ class TestParameterClient(unittest.TestCase): - def setUp(self): + def setUp(self) -> None: self.context = rclpy.context.Context() rclpy.init(context=self.context) self.client_node = rclpy.create_node( @@ -48,13 +49,13 @@ def setUp(self): self.executor.add_node(self.client_node) self.executor.add_node(self.target_node) - def tearDown(self): + def tearDown(self) -> None: self.executor.shutdown() self.client_node.destroy_node() self.target_node.destroy_node() rclpy.shutdown(context=self.context) - def test_set_parameter(self): + def test_set_parameter(self) -> None: future = self.client.set_parameters([ Parameter('int_param', Parameter.Type.INTEGER, 88).to_parameter_msg(), Parameter('string.param', Parameter.Type.STRING, 'hello world'), @@ -66,7 +67,7 @@ def test_set_parameter(self): res = [i.successful for i in results.results] assert all(res) - def test_get_parameter(self): + def test_get_parameter(self) -> None: future = self.client.get_parameters(['int_arr_param', 'float.param..']) self.executor.spin_until_future_complete(future) results = future.result() @@ -75,7 +76,7 @@ def test_get_parameter(self): assert list(results.values[0].integer_array_value) == [1, 2, 3] assert results.values[1].double_value == 3.14 - def test_list_parameters(self): + def test_list_parameters(self) -> None: future = self.client.list_parameters() self.executor.spin_until_future_complete(future) results = future.result() @@ -91,7 +92,7 @@ def test_list_parameters(self): assert 'float.param..' in results.result.names assert 'float.param.' in results.result.prefixes - def test_describe_parameters(self): + def test_describe_parameters(self) -> None: future = self.client.describe_parameters(['int_arr_param']) self.executor.spin_until_future_complete(future) results = future.result() @@ -100,7 +101,7 @@ def test_describe_parameters(self): assert results.descriptors[0].type == ParameterType.PARAMETER_INTEGER_ARRAY assert results.descriptors[0].name == 'int_arr_param' - def test_get_paramter_types(self): + def test_get_paramter_types(self) -> None: future = self.client.get_parameter_types(['int_arr_param']) self.executor.spin_until_future_complete(future) results = future.result() @@ -108,7 +109,7 @@ def test_get_paramter_types(self): assert len(results.types) == 1 assert results.types[0] == ParameterType.PARAMETER_INTEGER_ARRAY - def test_set_parameters_atomically(self): + def test_set_parameters_atomically(self) -> None: future = self.client.set_parameters_atomically([ Parameter('int_param', Parameter.Type.INTEGER, 888), Parameter('string.param', Parameter.Type.STRING, 'Hello World').to_parameter_msg(), @@ -118,7 +119,7 @@ def test_set_parameters_atomically(self): assert results is not None assert results.result.successful - def test_delete_parameters(self): + def test_delete_parameters(self) -> None: self.target_node.declare_parameter('delete_param', 10) descriptor = rcl_interfaces.msg.ParameterDescriptor(dynamic_typing=True) self.target_node.declare_parameter('delete_param_dynamic', 10, descriptor=descriptor) @@ -138,7 +139,7 @@ def test_delete_parameters(self): assert len(result.results) == 1 assert result.results[0].successful - def test_load_parameter_file(self): + def test_load_parameter_file(self) -> None: yaml_string = """/param_test_target: ros__parameters: param_1: 1 @@ -159,7 +160,7 @@ def test_load_parameter_file(self): if os.path.exists(f.name): os.unlink(f.name) - def test_load_parameter_file_atomically(self): + def test_load_parameter_file_atomically(self) -> None: yaml_string = """/param_test_target: ros__parameters: param_1: 1 @@ -179,7 +180,7 @@ def test_load_parameter_file_atomically(self): if os.path.exists(f.name): os.unlink(f.name) - def test_get_uninitialized_parameter(self): + def test_get_uninitialized_parameter(self) -> None: self.target_node.declare_parameter('uninitialized_parameter', Parameter.Type.STRING) # The type in description should be STRING @@ -200,8 +201,8 @@ def test_get_uninitialized_parameter(self): self.target_node.undeclare_parameter('uninitialized_parameter') - def test_on_parameter_event_new(self): - def on_new_parameter_event(msg): + def test_on_parameter_event_new(self) -> None: + def on_new_parameter_event(msg: ParameterEvent) -> None: assert msg.node == '/rclpy/test_parameter_client_target' assert len(msg.new_parameters) == 1 assert msg.new_parameters[0].name == 'int_param' @@ -223,7 +224,7 @@ def on_new_parameter_event(msg): param_event_sub.destroy() - def test_on_parameter_event_changed(self): + def test_on_parameter_event_changed(self) -> None: future = self.client.set_parameters([ Parameter('int_param', Parameter.Type.INTEGER, 88).to_parameter_msg(), ]) @@ -234,7 +235,7 @@ def test_on_parameter_event_changed(self): res = [i.successful for i in results.results] assert all(res) - def on_changed_parameter_event(msg): + def on_changed_parameter_event(msg: ParameterEvent) -> None: assert msg.node == '/rclpy/test_parameter_client_target' assert len(msg.new_parameters) == 0 assert len(msg.changed_parameters) == 1 @@ -256,7 +257,7 @@ def on_changed_parameter_event(msg): param_event_sub.destroy() - def test_on_parameter_event_deleted(self): + def test_on_parameter_event_deleted(self) -> None: future = self.client.set_parameters([ Parameter('int_param', Parameter.Type.INTEGER, 88).to_parameter_msg(), ]) @@ -267,7 +268,7 @@ def test_on_parameter_event_deleted(self): res = [i.successful for i in results.results] assert all(res) - def on_deleted_parameter_event(msg): + def on_deleted_parameter_event(msg: ParameterEvent) -> None: assert msg.node == '/rclpy/test_parameter_client_target' assert len(msg.new_parameters) == 0 assert len(msg.changed_parameters) == 0