Skip to content

Commit

Permalink
minor improvements (#1330)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
  • Loading branch information
InvincibleRMC authored Aug 7, 2024
1 parent aacf8f0 commit 8999302
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
24 changes: 13 additions & 11 deletions rclpy/rclpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
self,
context: Context,
client_impl: _rclpy.Client,
srv_type: Srv[SrvRequestT, SrvResponseT, SrvEventT],
srv_type: Type[Srv[SrvRequestT, SrvResponseT, SrvEventT]],
srv_name: str,
qos_profile: QoSProfile,
callback_group: CallbackGroup
Expand All @@ -66,7 +66,7 @@ def __init__(
self.srv_name = srv_name
self.qos_profile = qos_profile
# Key is a sequence number, value is an instance of a Future
self._pending_requests: Dict[int, Future] = {}
self._pending_requests: Dict[int, Future[SrvResponseT]] = {}
self.callback_group = callback_group
# True when the callback is ready to fire but has not been "taken" by an executor
self._executor_event = False
Expand Down Expand Up @@ -95,7 +95,7 @@ def call(

event = threading.Event()

def unblock(future):
def unblock(future: Future[SrvResponseT]) -> None:
nonlocal event
event.set()

Expand All @@ -109,11 +109,13 @@ def unblock(future):
if not event.wait(timeout_sec):
# Timed out. remove_pending_request() to free resources
self.remove_pending_request(future)
if future.exception() is not None:
raise future.exception()

exception = future.exception()
if exception is not None:
raise exception
return future.result()

def call_async(self, request: SrvRequestT) -> Future:
def call_async(self, request: SrvRequestT) -> Future[SrvResponseT]:
"""
Make a service request and asynchronously get the result.
Expand All @@ -132,14 +134,14 @@ def call_async(self, request: SrvRequestT) -> Future:
if sequence_number in self._pending_requests:
raise RuntimeError(f'Sequence ({sequence_number}) conflicts with pending request')

future = Future()
future = Future[SrvResponseT]()
self._pending_requests[sequence_number] = future

future.add_done_callback(self.remove_pending_request)

return future

def get_pending_request(self, sequence_number: int) -> Future:
def get_pending_request(self, sequence_number: int) -> Future[SrvResponseT]:
"""
Get a future from the list of pending requests.
Expand All @@ -150,7 +152,7 @@ def get_pending_request(self, sequence_number: int) -> Future:
with self._lock:
return self._pending_requests[sequence_number]

def remove_pending_request(self, future: Future) -> None:
def remove_pending_request(self, future: Future[SrvResponseT]) -> None:
"""
Remove a future from the list of pending requests.
Expand Down Expand Up @@ -220,7 +222,7 @@ def service_name(self) -> str:
with self.handle:
return self.__client.service_name

def destroy(self):
def destroy(self) -> None:
"""
Destroy a container for a ROS service client.
Expand All @@ -229,7 +231,7 @@ def destroy(self):
"""
self.__client.destroy_when_not_in_use()

def __enter__(self) -> 'Client':
def __enter__(self) -> 'Client[SrvRequestT, SrvResponseT, SrvEventT]':
return self

def __exit__(
Expand Down
4 changes: 2 additions & 2 deletions rclpy/rclpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ def create_subscription(

def create_client(
self,
srv_type: Srv[SrvRequestT, SrvResponseT, SrvEventT],
srv_type: Type[Srv[SrvRequestT, SrvResponseT, SrvEventT]],
srv_name: str,
*,
qos_profile: QoSProfile = qos_profile_services_default,
Expand Down Expand Up @@ -1719,7 +1719,7 @@ def create_client(

def create_service(
self,
srv_type: Srv[SrvRequestT, SrvResponseT, SrvEventT],
srv_type: Type[Srv[SrvRequestT, SrvResponseT, SrvEventT]],
srv_name: str,
callback: Callable[[SrvRequestT, SrvResponseT], SrvResponseT],
*,
Expand Down
6 changes: 3 additions & 3 deletions rclpy/rclpy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Service(Generic[SrvRequestT, SrvResponseT, SrvEventT]):
def __init__(
self,
service_impl: _rclpy.Service,
srv_type: Srv[SrvRequestT, SrvResponseT, SrvEventT],
srv_type: Type[Srv[SrvRequestT, SrvResponseT, SrvEventT]],
srv_name: str,
callback: Callable[[SrvRequestT, SrvResponseT], SrvResponseT],
callback_group: CallbackGroup,
Expand Down Expand Up @@ -112,7 +112,7 @@ def service_name(self) -> str:
with self.handle:
return self.__service.name

def destroy(self):
def destroy(self) -> None:
"""
Destroy a container for a ROS service server.
Expand All @@ -121,7 +121,7 @@ def destroy(self):
"""
self.__service.destroy_when_not_in_use()

def __enter__(self) -> 'Service':
def __enter__(self) -> 'Service[SrvRequestT, SrvResponseT, SrvEventT]':
return self

def __exit__(
Expand Down
3 changes: 1 addition & 2 deletions rclpy/rclpy/type_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class Srv(Protocol[SrvRequestT, SrvResponseT, SrvEventT], metaclass=CommonMsgSrv
Response: Type[SrvResponseT]
Event: Type[SrvEventT]

def __init__(self) -> NoReturn:
...
def __init__(self) -> NoReturn: ...


# Can be used if https://github.com/python/typing/issues/548 ever gets approved.
Expand Down

0 comments on commit 8999302

Please sign in to comment.