From 55be528a93b91d035367d00f8d25571338ec9410 Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Thu, 26 Sep 2024 12:53:03 -0400 Subject: [PATCH] lint, typecheck --- src/viam/components/board/client.py | 28 +++++++++++----------- src/viam/components/input/client.py | 6 ++--- src/viam/components/pose_tracker/client.py | 2 +- src/viam/components/sensor/client.py | 2 +- src/viam/components/servo/client.py | 2 +- src/viam/resource/rpc_client_base.py | 5 ++-- src/viam/resource/types.py | 2 +- src/viam/services/sensors/client.py | 8 ++++++- src/viam/services/vision/client.py | 3 ++- 9 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/viam/components/board/client.py b/src/viam/components/board/client.py index ab3ff53fc..464647ccd 100644 --- a/src/viam/components/board/client.py +++ b/src/viam/components/board/client.py @@ -27,7 +27,7 @@ StreamTicksResponse, WriteAnalogRequest, ) -from viam.resource.rpc_client_base import ReconfigurableResourceRPCClientBase +from viam.resource.rpc_client_base import ReconfigurableResourceRPCClientBase, ResourceRPCClientBase from viam.streams import StreamWithIterator from viam.utils import ValueTypes, dict_to_struct, get_geometries, struct_to_dict @@ -48,7 +48,7 @@ async def read( timeout: Optional[float] = None, **kwargs, ) -> Board.Analog.Value: - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = ReadAnalogReaderRequest(board_name=self.board.name, analog_reader_name=self.name, extra=dict_to_struct(extra)) return await self.board.client.ReadAnalogReader(request, timeout=timeout, metadata=md) @@ -60,7 +60,7 @@ async def write( timeout: Optional[float] = None, **kwargs, ): - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = WriteAnalogRequest(name=self.board.name, pin=self.name, value=value, extra=dict_to_struct(extra)) await self.board.client.WriteAnalog(request, timeout=timeout, metadata=md) @@ -77,7 +77,7 @@ async def value( timeout: Optional[float] = None, **kwargs, ) -> int: - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = GetDigitalInterruptValueRequest(board_name=self.board.name, digital_interrupt_name=self.name, extra=dict_to_struct(extra)) response: GetDigitalInterruptValueResponse = await self.board.client.GetDigitalInterruptValue(request, timeout=timeout, metadata=md) return response.value @@ -95,7 +95,7 @@ async def get( timeout: Optional[float] = None, **kwargs, ) -> bool: - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = GetGPIORequest(name=self.board.name, pin=self.name, extra=dict_to_struct(extra)) response: GetGPIOResponse = await self.board.client.GetGPIO(request, timeout=timeout, metadata=md) return response.high @@ -108,7 +108,7 @@ async def set( timeout: Optional[float] = None, **kwargs, ): - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = SetGPIORequest(name=self.board.name, pin=self.name, high=high, extra=dict_to_struct(extra)) await self.board.client.SetGPIO(request, timeout=timeout, metadata=md) @@ -119,7 +119,7 @@ async def get_pwm( timeout: Optional[float] = None, **kwargs, ) -> float: - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = PWMRequest(name=self.board.name, pin=self.name, extra=dict_to_struct(extra)) response: PWMResponse = await self.board.client.PWM(request, timeout=timeout, metadata=md) return response.duty_cycle_pct @@ -132,7 +132,7 @@ async def set_pwm( timeout: Optional[float] = None, **kwargs, ): - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = SetPWMRequest(name=self.board.name, pin=self.name, duty_cycle_pct=duty_cycle, extra=dict_to_struct(extra)) await self.board.client.SetPWM(request, timeout=timeout, metadata=md) @@ -143,7 +143,7 @@ async def get_pwm_frequency( timeout: Optional[float] = None, **kwargs, ) -> int: - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = PWMFrequencyRequest(name=self.board.name, pin=self.name, extra=dict_to_struct(extra)) response: PWMFrequencyResponse = await self.board.client.PWMFrequency(request, timeout=timeout, metadata=md) return response.frequency_hz @@ -156,7 +156,7 @@ async def set_pwm_frequency( timeout: Optional[float] = None, **kwargs, ): - md = kwargs.get('metadata', self.Metadata()).proto + md = kwargs.get('metadata', ResourceRPCClientBase.Metadata()).proto request = SetPWMFrequencyRequest(name=self.board.name, pin=self.name, frequency_hz=frequency, extra=dict_to_struct(extra)) await self.board.client.SetPWMFrequency(request, timeout=timeout, metadata=md) @@ -226,7 +226,7 @@ async def set_power_mode( async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]: md = kwargs.get('metadata', self.Metadata()) - return await get_geometries(self.client, self.name, extra, timeout) + return await get_geometries(self.client, self.name, extra, timeout, md) async def write_analog( self, @@ -248,17 +248,17 @@ async def stream_ticks( extra: Optional[Dict[str, Any]] = None, **kwargs, ) -> TickStream: - md = kwargs.get('metadata', self.Metadata()).proto names = [] for di in interrupts: names.append(di.name) request = StreamTicksRequest(name=self.name, pin_names=names, extra=dict_to_struct(extra)) async def read(): + md = kwargs.get('metadata', self.Metadata()).proto tick_stream: ClientStream[StreamTicksRequest, StreamTicksResponse] - async with self.client.StreamTicks.open() as tick_stream: + async with self.client.StreamTicks.open(metadata=md) as tick_stream: try: - await tick_stream.send_message(request, end=True, metadata=md) + await tick_stream.send_message(request, end=True) async for tick in tick_stream: yield tick except Exception as e: diff --git a/src/viam/components/input/client.py b/src/viam/components/input/client.py index 78b51a288..c596f1fb2 100644 --- a/src/viam/components/input/client.py +++ b/src/viam/components/input/client.py @@ -133,7 +133,7 @@ async def _stream_events(self, metadata: ResourceRPCClientBase.Metadata): return md = metadata.proto - request = StreamEventsRequest(controller=self.name, events=[], extra=self._callback_extra, metadata=md) + request = StreamEventsRequest(controller=self.name, events=[], extra=self._callback_extra) with self._lock: for control, callbacks in self.callbacks.items(): event = StreamEventsRequest.Events( @@ -144,7 +144,7 @@ async def _stream_events(self, metadata: ResourceRPCClientBase.Metadata): request.events.append(event) try: - async with self.client.StreamEvents.open() as stream: + async with self.client.StreamEvents.open(metadata=md) as stream: await stream.send_message(request, end=True) self._send_connection_status(True) reply: StreamEventsResponse @@ -191,4 +191,4 @@ async def do_command( async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]: md = kwargs.get('metadata', self.Metadata()) - return await get_geometries(self.client, self.name, extra, timeout) + return await get_geometries(self.client, self.name, extra, timeout, md) diff --git a/src/viam/components/pose_tracker/client.py b/src/viam/components/pose_tracker/client.py index 6497a4367..c961e1078 100644 --- a/src/viam/components/pose_tracker/client.py +++ b/src/viam/components/pose_tracker/client.py @@ -47,4 +47,4 @@ async def do_command( async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]: md = kwargs.get('metadata', self.Metadata()) - return await get_geometries(self.client, self.name, extra, timeout) + return await get_geometries(self.client, self.name, extra, timeout, md) diff --git a/src/viam/components/sensor/client.py b/src/viam/components/sensor/client.py index d0ae33ffe..6de40acbd 100644 --- a/src/viam/components/sensor/client.py +++ b/src/viam/components/sensor/client.py @@ -46,4 +46,4 @@ async def do_command( async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]: md = kwargs.get('metadata', self.Metadata()) - return await get_geometries(self.client, self.name, extra, timeout) + return await get_geometries(self.client, self.name, extra, timeout, md) diff --git a/src/viam/components/servo/client.py b/src/viam/components/servo/client.py index 45b5a2d44..9c22209b0 100644 --- a/src/viam/components/servo/client.py +++ b/src/viam/components/servo/client.py @@ -83,4 +83,4 @@ async def do_command( async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[Geometry]: md = kwargs.get('metadata', self.Metadata()) - return await get_geometries(self.client, self.name, extra, timeout) + return await get_geometries(self.client, self.name, extra, timeout, md) diff --git a/src/viam/resource/rpc_client_base.py b/src/viam/resource/rpc_client_base.py index fd39f4d20..2b7025987 100644 --- a/src/viam/resource/rpc_client_base.py +++ b/src/viam/resource/rpc_client_base.py @@ -1,4 +1,4 @@ -from typing import Any, Mapping, Optional, Protocol, runtime_checkable +from typing import Any, Dict, Protocol, runtime_checkable from string import ascii_lowercase from random import choice @@ -15,7 +15,7 @@ class ResourceRPCClientBase(Protocol): """ class Metadata: - metadata: Mapping[str, str] = {} + metadata: Dict[str, str] = {} def enable_debug_logging(self, key: str = ''): """Enables server-side debug logging for resource methods. @@ -39,6 +39,7 @@ def proto(self): channel: Channel client: Any + class ReconfigurableResourceRPCClientBase(ResourceRPCClientBase): """A base RPC client that can reset its channel. diff --git a/src/viam/resource/types.py b/src/viam/resource/types.py index 588ce9257..633df9c4b 100644 --- a/src/viam/resource/types.py +++ b/src/viam/resource/types.py @@ -210,5 +210,5 @@ def resource_name_from_string(string: str) -> ResourceName: class SupportsGetGeometries(Protocol): """The SupportsGetGeometries protocol defines the requirements for a resource to call get_geometries.""" - async def GetGeometries(self, request: GetGeometriesRequest, *, timeout: Optional[float] = None) -> GetGeometriesResponse: + async def GetGeometries(self, request: GetGeometriesRequest, *, timeout: Optional[float] = None, **kwargs) -> GetGeometriesResponse: ... diff --git a/src/viam/services/sensors/client.py b/src/viam/services/sensors/client.py index 1522e0e91..f53a918bd 100644 --- a/src/viam/services/sensors/client.py +++ b/src/viam/services/sensors/client.py @@ -22,7 +22,13 @@ def __init__(self, name: str, channel: Channel): super().__init__(name, channel) self.client = SensorsServiceStub(channel) - async def get_sensors(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> List[ResourceName]: + async def get_sensors( + self, + *, + extra: Optional[Mapping[str, Any]] = None, + timeout: Optional[float] = None, + **kwargs, + ) -> List[ResourceName]: """Get the ``ResourceName`` of all the ``Sensor`` resources connected to this Robot Returns: diff --git a/src/viam/services/vision/client.py b/src/viam/services/vision/client.py index d725c6af3..481e1f516 100644 --- a/src/viam/services/vision/client.py +++ b/src/viam/services/vision/client.py @@ -129,7 +129,8 @@ async def get_classifications_from_camera( ) -> List[Classification]: md = kwargs.get('metadata', self.Metadata()).proto request = GetClassificationsFromCameraRequest(name=self.name, camera_name=camera_name, n=count, extra=dict_to_struct(extra)) - response: GetClassificationsFromCameraResponse = await self.client.GetClassificationsFromCamera(request, timeout=timeout, metadata=md) + response: GetClassificationsFromCameraResponse = await self.client.GetClassificationsFromCamera( + request, timeout=timeout, metadata=md) return list(response.classifications) async def get_classifications(