Skip to content

Commit 7bbc104

Browse files
RSDK 850 specify docommand value types (#226)
1 parent 982f35f commit 7bbc104

File tree

21 files changed

+109
-86
lines changed

21 files changed

+109
-86
lines changed

src/viam/components/arm/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Mapping, Optional
22

33
from grpclib.client import Channel
44

@@ -16,7 +16,7 @@
1616
MoveToPositionRequest,
1717
StopRequest,
1818
)
19-
from viam.utils import dict_to_struct, struct_to_dict
19+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
2020

2121
from .arm import Arm
2222

@@ -102,10 +102,10 @@ async def is_moving(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Op
102102

103103
async def do_command(
104104
self,
105-
command: Dict[str, Any],
105+
command: Mapping[str, Any],
106106
*,
107107
timeout: Optional[float] = None,
108-
) -> Dict[str, Any]:
108+
) -> Mapping[str, ValueTypes]:
109109
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
110110
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
111111
return struct_to_dict(response.result)

src/viam/components/audio_input/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, AsyncIterator, Dict, Optional, Union
1+
from typing import AsyncIterator, Mapping, Optional, Union
22

33
from grpclib.client import Channel
44

@@ -13,7 +13,7 @@
1313
PropertiesResponse,
1414
SampleFormat,
1515
)
16-
from viam.utils import dict_to_struct, struct_to_dict
16+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
1717

1818
from .audio_input import AudioInput
1919

@@ -53,7 +53,7 @@ async def get_properties(self, *, timeout: Optional[float] = None) -> AudioInput
5353
response: PropertiesResponse = await self.client.Properties(request, timeout=timeout)
5454
return AudioInput.Properties.from_proto(response)
5555

56-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
56+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
5757
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
5858
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
5959
return struct_to_dict(response.result)

src/viam/components/base/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Mapping, Optional
22

33
from grpclib.client import Channel
44

@@ -13,7 +13,7 @@
1313
SpinRequest,
1414
StopRequest,
1515
)
16-
from viam.utils import dict_to_struct, struct_to_dict
16+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
1717

1818
from .base import Base
1919

@@ -115,10 +115,10 @@ async def is_moving(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Op
115115

116116
async def do_command(
117117
self,
118-
command: Dict[str, Any],
118+
command: Mapping[str, ValueTypes],
119119
*,
120120
timeout: Optional[float] = None,
121-
) -> Dict[str, Any]:
121+
) -> Mapping[str, ValueTypes]:
122122
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
123123
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
124124
return struct_to_dict(response.result)

src/viam/components/board/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from multiprocessing import Queue
2-
from typing import Any, Dict, List, Optional
2+
from typing import Any, Dict, Mapping, List, Optional
33

44
from grpclib.client import Channel
55

@@ -22,7 +22,7 @@
2222
StatusRequest,
2323
StatusResponse,
2424
)
25-
from viam.utils import dict_to_struct, struct_to_dict
25+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
2626

2727
from .board import Board, PostProcessor
2828

@@ -152,7 +152,7 @@ async def status(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optio
152152
async def model_attributes(self) -> Board.Attributes:
153153
return Board.Attributes(remote=True)
154154

155-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
155+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
156156
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
157157
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
158158
return struct_to_dict(response.result)

src/viam/components/camera/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from io import BytesIO
2-
from typing import Any, Dict, Optional, Tuple, Union
2+
from typing import Mapping, Optional, Tuple, Union
33

44
from grpclib.client import Channel
55
from PIL import Image
@@ -15,7 +15,7 @@
1515
GetPropertiesRequest,
1616
GetPropertiesResponse,
1717
)
18-
from viam.utils import dict_to_struct, struct_to_dict
18+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
1919

2020
from .camera import Camera
2121

@@ -48,7 +48,7 @@ async def get_properties(self, *, timeout: Optional[float] = None) -> Camera.Pro
4848
response: GetPropertiesResponse = await self.client.GetProperties(GetPropertiesRequest(name=self.name), timeout=timeout)
4949
return Camera.Properties(response.supports_pcd, response.intrinsic_parameters, response.distortion_parameters)
5050

51-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
51+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
5252
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
5353
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
5454
return struct_to_dict(response.result)

src/viam/components/component_base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import abc
2-
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Mapping, Optional, cast
2+
from typing import TYPE_CHECKING, Any, SupportsBytes, ClassVar, SupportsFloat, List, Mapping, Optional, cast, Union
33

44
from typing_extensions import Self
55

@@ -11,6 +11,9 @@
1111
from viam.robot.client import RobotClient
1212

1313

14+
ValueTypes = Union[bool, SupportsBytes, SupportsFloat, List, Mapping, str, None]
15+
16+
1417
class ComponentBase(abc.ABC):
1518
"""
1619
Base component.
@@ -68,16 +71,16 @@ def get_operation(self, kwargs: Mapping[str, Any]) -> Operation:
6871
"""
6972
return kwargs.get(Operation.ARG_NAME, Operation._noop())
7073

71-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None, **kwargs) -> Dict[str, Any]:
74+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
7275
"""Send/Receive arbitrary commands
7376
7477
Args:
75-
command (Dict[str, Any]): The command to execute
78+
command (Mapping[str, ValueTypes]): The command to execute
7679
7780
Raises:
7881
NotImplementedError: Raised if the component does not support arbitrary commands
7982
8083
Returns:
81-
Dict[str, Any]: Result of the executed command
84+
Mapping[str, ValueTypes]: Result of the executed command
8285
"""
8386
raise NotImplementedError()

src/viam/components/gantry/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Optional
1+
from typing import Any, Dict, List, Mapping, Optional
22

33
from grpclib.client import Channel
44

@@ -14,7 +14,7 @@
1414
MoveToPositionRequest,
1515
StopRequest,
1616
)
17-
from viam.utils import dict_to_struct, struct_to_dict
17+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
1818

1919
from .gantry import Gantry
2020

@@ -69,7 +69,7 @@ async def is_moving(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Op
6969
response: IsMovingResponse = await self.client.IsMoving(request, timeout=timeout)
7070
return response.is_moving
7171

72-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
72+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
7373
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
7474
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
7575
return struct_to_dict(response.result)

src/viam/components/generic/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Mapping, Optional
22
from grpclib import GRPCError, Status
33
from grpclib.client import Channel
44
from viam.proto.component.generic import GenericServiceStub, DoCommandRequest, DoCommandResponse
5-
from viam.utils import dict_to_struct, struct_to_dict
5+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
66

77
from .generic import Generic
88

@@ -16,7 +16,7 @@ def __init__(self, name: str, channel: Channel):
1616
self.client = GenericServiceStub(channel)
1717
super().__init__(name)
1818

19-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
19+
async def do_command(self, command: Mapping[str, Any], *, timeout: Optional[float] = None) -> Mapping[str, Any]:
2020
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
2121
try:
2222
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
@@ -28,7 +28,9 @@ async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float]
2828
return struct_to_dict(response.result)
2929

3030

31-
async def do_command(channel: Channel, name: str, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
31+
async def do_command(
32+
channel: Channel, name: str, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None
33+
) -> Mapping[str, ValueTypes]:
3234
"""Convenience method to allow component clients to execute ``do_command`` functions
3335
3436
Args:

src/viam/components/gripper/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Mapping, Optional
22

33
from grpclib.client import Channel
44

@@ -12,7 +12,7 @@
1212
OpenRequest,
1313
StopRequest,
1414
)
15-
from viam.utils import dict_to_struct, struct_to_dict
15+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
1616

1717
from .gripper import Gripper
1818

@@ -53,7 +53,7 @@ async def is_moving(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Op
5353
response: IsMovingResponse = await self.client.IsMoving(request, timeout=timeout)
5454
return response.is_moving
5555

56-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
56+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
5757
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
5858
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
5959
return struct_to_dict(response.result)

src/viam/components/input/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from threading import Lock
33
from time import time
4-
from typing import Any, Dict, List, Optional
4+
from typing import Any, Dict, Mapping, List, Optional
55
from grpclib import GRPCError, Status
66
from google.protobuf.struct_pb2 import Struct
77

@@ -20,7 +20,7 @@
2020
StreamEventsResponse,
2121
TriggerEventRequest,
2222
)
23-
from viam.utils import dict_to_struct, struct_to_dict
23+
from viam.utils import dict_to_struct, struct_to_dict, ValueTypes
2424

2525
from .input import Control, ControlFunction, Controller, Event, EventType
2626

@@ -147,7 +147,7 @@ def _execute_callback(self, event: Event):
147147
if all_callback is not None:
148148
all_callback(event)
149149

150-
async def do_command(self, command: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
150+
async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None) -> Mapping[str, ValueTypes]:
151151
request = DoCommandRequest(name=self.name, command=dict_to_struct(command))
152152
response: DoCommandResponse = await self.client.DoCommand(request, timeout=timeout)
153153
return struct_to_dict(response.result)

0 commit comments

Comments
 (0)