Skip to content

Commit

Permalink
pw_rpc: Require an output function for channels
Browse files Browse the repository at this point in the history
- The code already assumed the output function was not None, so require
  an output function.
- Remove type: ignore comments that were no longer necessary.

Change-Id: I9a43d3c49f401c82c7a4cdb47a1798866853eccd
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/227855
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Aug 13, 2024
1 parent 7ef5e1f commit d33bfc0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
20 changes: 6 additions & 14 deletions pw_rpc/py/pw_rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ def send_request(
previous = self.open(rpc, context, override_pending)
packet = packets.encode_request(rpc, request)

# TODO(hepler): Remove `type: ignore[misc]` below when
# https://github.com/python/mypy/issues/10711 is fixed.
if ignore_errors:
try:
rpc.channel.output(packet) # type: ignore[misc]
rpc.channel.output(packet)
except Exception as err: # pylint: disable=broad-except
_LOG.debug('Ignoring exception when starting RPC: %s', err)
else:
rpc.channel.output(packet) # type: ignore[misc]
rpc.channel.output(packet)

return previous

Expand Down Expand Up @@ -168,19 +166,15 @@ def send_client_stream(self, rpc: PendingRpc, message: Message) -> None:
if rpc not in self._pending:
raise Error(f'Attempt to send client stream for inactive RPC {rpc}')

rpc.channel.output( # type: ignore
packets.encode_client_stream(rpc, message)
)
rpc.channel.output(packets.encode_client_stream(rpc, message))

def send_client_stream_end(self, rpc: PendingRpc) -> None:
if rpc not in self._pending:
raise Error(
f'Attempt to send client stream end for inactive RPC {rpc}'
)

rpc.channel.output( # type: ignore
packets.encode_client_stream_end(rpc)
)
rpc.channel.output(packets.encode_client_stream_end(rpc))

def cancel(self, rpc: PendingRpc) -> bytes:
"""Cancels the RPC.
Expand All @@ -204,7 +198,7 @@ def send_cancel(self, rpc: PendingRpc) -> bool:
return False

if packet:
rpc.channel.output(packet) # type: ignore
rpc.channel.output(packet)

return True

Expand Down Expand Up @@ -669,6 +663,4 @@ def _send_client_error(
) -> None:
# Never send responses to SERVER_ERRORs.
if packet.type != PacketType.SERVER_ERROR:
client.channel.output( # type: ignore
packets.encode_client_error(packet, error)
)
client.channel.output(packets.encode_client_error(packet, error))
2 changes: 1 addition & 1 deletion pw_rpc/py/pw_rpc/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@dataclass(frozen=True)
class Channel:
id: int
output: Callable[[bytes], Any] | None
output: Callable[[bytes], Any]

def __repr__(self) -> str:
return f'Channel({self.id})'
Expand Down
2 changes: 1 addition & 1 deletion pw_rpc/py/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def create_protos() -> Any:

def create_client(
proto_modules: Any,
first_channel_output_fn: Callable[[bytes], Any] | None = None,
first_channel_output_fn: Callable[[bytes], Any] = lambda _: None,
) -> client.Client:
return client.Client.from_modules(
callback_client.Impl(),
Expand Down

0 comments on commit d33bfc0

Please sign in to comment.