Skip to content

RSDK-2870: use api instead of subtype #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The SDK provides a number of abstract base components and services (collectively
1. Define all requirements of the resource in `{RESOURCE_NAME}.py`
1. Implement the gRPC service for the new resource in `service.py`
1. Create a gRPC client for the new resource in `client.py`
1. Register the subtype and define package exports in `__init__.py`
1. Register the API and define package exports in `__init__.py`
1. Write tests for the new resource and add the resource to `tests.mocks.{components|services}`
1. If the resource is a component, add the component to `examples.server.v1.components` and its corresponding concrete type in `examples.server.v1.server`

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/module_step2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def close(self):


async def main():
Registry.register_resource_creator(Sensor.SUBTYPE, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new))
Registry.register_resource_creator(Sensor.API, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/module_step2_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def close(self):


async def main():
Registry.register_resource_creator(Sensor.SUBTYPE, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new, MySensor.validate_config))
Registry.register_resource_creator(Sensor.API, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new, MySensor.validate_config))


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/module_step3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ async def main():
This function creates and starts a new module, after adding all desired resource model.
Resource creators must be registered to the resource registry before the module adds the resource model.
"""
Registry.register_resource_creator(Sensor.SUBTYPE, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new))
Registry.register_resource_creator(Sensor.API, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new))

module = Module.from_args()
module.add_model_from_registry(Sensor.SUBTYPE, MySensor.MODEL)
module.add_model_from_registry(Sensor.API, MySensor.MODEL)
await module.start()


Expand Down
2 changes: 1 addition & 1 deletion examples/complex_module/src/arm/my_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ async def close(self):
LOGGER.info(f"{self.name} is closed.")


Registry.register_resource_creator(Arm.SUBTYPE, MyArm.MODEL, ResourceCreatorRegistration(MyArm.new))
Registry.register_resource_creator(Arm.API, MyArm.MODEL, ResourceCreatorRegistration(MyArm.new))
4 changes: 2 additions & 2 deletions examples/complex_module/src/base/my_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MyBase(Base, Reconfigurable):
MyBase implements a base that only supports set_power (basic forward/back/turn controls), is_moving (check if in motion), and stop (stop
all motion).

It inherits from the built-in resource subtype Base and conforms to the ``Reconfigurable`` protocol, which signifies that this component
It inherits from the built-in resource API Base and conforms to the ``Reconfigurable`` protocol, which signifies that this component
can be reconfigured. Additionally, it specifies a constructor function ``MyBase.new`` which conforms to the
``resource.types.ResourceCreator`` type required for all models. It also specifies a validator function `MyBase.validate_config` which
conforms to the ``resource.types.Validator`` type and returns implicit dependencies for the model.
Expand Down Expand Up @@ -147,4 +147,4 @@ async def get_geometries(self) -> List[Geometry]:
raise NotImplementedError()


Registry.register_resource_creator(Base.SUBTYPE, MyBase.MODEL, ResourceCreatorRegistration(MyBase.new, MyBase.validate_config))
Registry.register_resource_creator(Base.API, MyBase.MODEL, ResourceCreatorRegistration(MyBase.new, MyBase.validate_config))
4 changes: 2 additions & 2 deletions examples/complex_module/src/gizmo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
This file registers the Gizmo subtype with the Viam Registry, as well as the specific MyGizmo model.
This file registers the Gizmo API with the Viam Registry, as well as the specific MyGizmo model.
"""

from viam.components.motor import * # noqa: F403 Need to import motor so the component registers itself
from viam.resource.registry import Registry, ResourceRegistration

from .api import Gizmo, GizmoClient, GizmoService

Registry.register_subtype(ResourceRegistration(Gizmo, GizmoService, lambda name, channel: GizmoClient(name, channel)))
Registry.register_api(ResourceRegistration(Gizmo, GizmoService, lambda name, channel: GizmoClient(name, channel)))
6 changes: 3 additions & 3 deletions examples/complex_module/src/gizmo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
and the gRPC client that will be able to make calls to this component.

In this example, the ``Gizmo`` abstract class defines what functionality is required for all Gizmos. It extends ``ComponentBase``,
as all component types must. It also defines its specific ``SUBTYPE``, which is used internally to keep track of supported types.
as all component types must. It also defines its specific ``API``, which is used internally to keep track of supported types.

The ``GizmoService`` implements the gRPC service for the Gizmo. This will allow other robots and clients to make requests of the Gizmo.
It extends both from ``GizmoServiceBase`` and ``ResourceRPCServiceBase``. The former is the gRPC service as defined by the proto,
Expand All @@ -28,7 +28,7 @@
from viam.components.component_base import ComponentBase
from viam.components.generic.client import do_command
from viam.resource.rpc_service_base import ResourceRPCServiceBase
from viam.resource.types import RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_TYPE_COMPONENT, API
from viam.utils import ValueTypes

from ..proto.gizmo_grpc import GizmoServiceBase, GizmoServiceStub
Expand All @@ -49,7 +49,7 @@
class Gizmo(ComponentBase):
"""Example component to use with the example module."""

SUBTYPE: Final = Subtype("acme", RESOURCE_TYPE_COMPONENT, "gizmo")
API: Final = API("acme", RESOURCE_TYPE_COMPONENT, "gizmo")

@abc.abstractmethod
async def do_one(self, arg1: str, **kwargs) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions examples/complex_module/src/gizmo/my_gizmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def validate_config(cls, config: ComponentConfig) -> Sequence[str]:
# can raise errors that will be returned to the parent through gRPC. Validate functions can
# also return a sequence of strings representing the implicit dependencies of the resource.
if "invalid" in config.attributes.fields:
raise Exception(f"'invalid' attribute not allowed for model {cls.SUBTYPE}:{cls.MODEL}")
raise Exception(f"'invalid' attribute not allowed for model {cls.API}:{cls.MODEL}")
arg1 = config.attributes.fields["arg1"].string_value
if arg1 == "":
raise Exception("A arg1 attribute is required for Gizmo component.")
Expand Down Expand Up @@ -79,4 +79,4 @@ async def close(self):
LOGGER.info(f"{self.name} is closed.")


Registry.register_resource_creator(Gizmo.SUBTYPE, MyGizmo.MODEL, ResourceCreatorRegistration(MyGizmo.new, MyGizmo.validate_config))
Registry.register_resource_creator(Gizmo.API, MyGizmo.MODEL, ResourceCreatorRegistration(MyGizmo.new, MyGizmo.validate_config))
8 changes: 4 additions & 4 deletions examples/complex_module/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ async def main():
Resource models must be pre-registered. For an example, see the `gizmo.__init__.py` file.
"""
module = Module.from_args()
module.add_model_from_registry(Gizmo.SUBTYPE, MyGizmo.MODEL)
module.add_model_from_registry(SummationService.SUBTYPE, MySummationService.MODEL)
module.add_model_from_registry(Arm.SUBTYPE, MyArm.MODEL)
module.add_model_from_registry(Base.SUBTYPE, MyBase.MODEL)
module.add_model_from_registry(Gizmo.API, MyGizmo.MODEL)
module.add_model_from_registry(SummationService.API, MySummationService.MODEL)
module.add_model_from_registry(Arm.API, MyArm.MODEL)
module.add_model_from_registry(Base.API, MyBase.MODEL)
await module.start()


Expand Down
4 changes: 2 additions & 2 deletions examples/complex_module/src/summation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
This file registers the Summation subtype with the Viam Registry, as well as the specific MySummation model.
This file registers the Summation API with the Viam Registry, as well as the specific MySummation model.
"""

from viam.resource.registry import Registry, ResourceRegistration

from .api import SummationClient, SummationRPCService, SummationService

Registry.register_subtype(ResourceRegistration(SummationService, SummationRPCService, lambda name, channel: SummationClient(name, channel)))
Registry.register_api(ResourceRegistration(SummationService, SummationRPCService, lambda name, channel: SummationClient(name, channel)))
6 changes: 3 additions & 3 deletions examples/complex_module/src/summation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

In this example, the ``Summation`` abstract class defines what functionality is required for all Summation services.
It extends ``ServiceBase``, as all service types must.
It also defines its specific ``SUBTYPE``, which is used internally to keep track of supported types.
It also defines its specific ``API``, which is used internally to keep track of supported types.

The ``SummationRPCService`` implements the gRPC service for the Summation service. This will allow other robots and clients to make
requests of the Summation service. It extends both from ``SummationServiceBase`` and ``RPCServiceBase``.
Expand All @@ -27,7 +27,7 @@
from grpclib.server import Stream

from viam.resource.rpc_service_base import ResourceRPCServiceBase
from viam.resource.types import RESOURCE_TYPE_SERVICE, Subtype
from viam.resource.types import RESOURCE_TYPE_SERVICE, API
from viam.services.service_base import ServiceBase

from ..proto.summation_grpc import SummationServiceBase, SummationServiceStub
Expand All @@ -37,7 +37,7 @@
class SummationService(ServiceBase):
"""Example service to use with the example module"""

SUBTYPE: Final = Subtype("acme", RESOURCE_TYPE_SERVICE, "summation")
API: Final = API("acme", RESOURCE_TYPE_SERVICE, "summation")

@abc.abstractmethod
async def sum(self, nums: Sequence[float]) -> float:
Expand Down
2 changes: 1 addition & 1 deletion examples/complex_module/src/summation/my_summation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def reconfigure(self, config: ComponentConfig, dependencies: Mapping[ResourceNam
self.subtract = config.attributes.fields["subtract"].bool_value or False


Registry.register_resource_creator(SummationService.SUBTYPE, MySummationService.MODEL, ResourceCreatorRegistration(MySummationService.new))
Registry.register_resource_creator(SummationService.API, MySummationService.MODEL, ResourceCreatorRegistration(MySummationService.new))
4 changes: 2 additions & 2 deletions examples/simple_module/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ async def main():
"""This function creates and starts a new module, after adding all desired resource models.
Resource creators must be registered to the resource registry before the module adds the resource model.
"""
Registry.register_resource_creator(Sensor.SUBTYPE, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new, MySensor.validate_config))
Registry.register_resource_creator(Sensor.API, MySensor.MODEL, ResourceCreatorRegistration(MySensor.new, MySensor.validate_config))

module = Module.from_args()
module.add_model_from_registry(Sensor.SUBTYPE, MySensor.MODEL)
module.add_model_from_registry(Sensor.API, MySensor.MODEL)
await module.start()


Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/arm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"Pose",
]

Registry.register_subtype(ResourceRegistration(Arm, ArmRPCService, lambda name, channel: ArmClient(name, channel)))
Registry.register_api(ResourceRegistration(Arm, ArmRPCService, lambda name, channel: ArmClient(name, channel)))
4 changes: 2 additions & 2 deletions src/viam/components/arm/arm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
from typing import Any, Dict, Final, Optional, Tuple

from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API

from ..component_base import ComponentBase
from . import JointPositions, KinematicsFileFormat, Pose
Expand All @@ -26,7 +26,7 @@ class Arm(ComponentBase):
For more information, see `Arm component <https://docs.viam.com/components/arm/>`_.
"""

SUBTYPE: Final = Subtype(RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "arm") # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API(RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "arm") # pyright: ignore [reportIncompatibleVariableOverride]

@abc.abstractmethod
async def get_end_position(
Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/audio_input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]


Registry.register_subtype(
Registry.register_api(
ResourceRegistration(
AudioInput,
AudioInputRPCService,
Expand Down
4 changes: 2 additions & 2 deletions src/viam/components/audio_input/audio_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from viam.media.audio import Audio, AudioStream
from viam.proto.component.audioinput import PropertiesResponse
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API
from viam.streams import StreamSource

from ..component_base import ComponentBase
Expand All @@ -22,7 +22,7 @@ class AudioInput(ComponentBase, StreamSource[Audio]):
overridden, it must call the ``super().__init__()`` function.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "audio_input"
)

Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"Vector3",
]

Registry.register_subtype(ResourceRegistration(Base, BaseRPCService, lambda name, channel: BaseClient(name, channel)))
Registry.register_api(ResourceRegistration(Base, BaseRPCService, lambda name, channel: BaseClient(name, channel)))
4 changes: 2 additions & 2 deletions src/viam/components/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass
from typing import Any, Dict, Final, Optional

from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API

from ..component_base import ComponentBase
from . import Vector3
Expand All @@ -23,7 +23,7 @@ class Base(ComponentBase):
For more information, see `Base component <https://docs.viam.com/components/base/>`_.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "base"
)

Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/board/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

__all__ = ["Board", "Tick", "TickStream"]

Registry.register_subtype(ResourceRegistration(Board, BoardRPCService, lambda name, channel: BoardClient(name, channel)))
Registry.register_api(ResourceRegistration(Board, BoardRPCService, lambda name, channel: BoardClient(name, channel)))
4 changes: 2 additions & 2 deletions src/viam/components/board/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Dict, Final, List, Optional

from viam.proto.component.board import PowerMode, ReadAnalogReaderResponse, StreamTicksResponse
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API
from viam.streams import Stream

from ..component_base import ComponentBase
Expand Down Expand Up @@ -34,7 +34,7 @@ class Board(ComponentBase):
For more information, see `Board component <https://docs.viam.com/components/board/>`_.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "board"
)

Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ViamImage",
]

Registry.register_subtype(
Registry.register_api(
ResourceRegistration(
Camera,
CameraRPCService,
Expand Down
4 changes: 2 additions & 2 deletions src/viam/components/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from viam.media.video import NamedImage, ViamImage
from viam.proto.common import ResponseMetadata
from viam.proto.component.camera import GetPropertiesResponse
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API

from ..component_base import ComponentBase

Expand All @@ -30,7 +30,7 @@ class Camera(ComponentBase):
For more information, see `Camera component <https://docs.viam.com/components/camera/>`_.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "camera"
)

Expand Down
6 changes: 3 additions & 3 deletions src/viam/components/component_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from viam.resource.base import ResourceBase

if TYPE_CHECKING:
from viam.resource.types import Subtype
from viam.resource.types import API
from viam.robot.client import RobotClient


Expand All @@ -23,11 +23,11 @@ class ComponentBase(abc.ABC, ResourceBase):
All components must inherit from this class.
"""

SUBTYPE: ClassVar["Subtype"]
API: ClassVar["API"]

def __init__(self, name: str, *, logger: Optional[Logger] = None):
self.name = name
self.logger = logger if logger is not None else getLogger(f"{self.SUBTYPE}.{name}")
self.logger = logger if logger is not None else getLogger(f"{self.API}.{name}")

@classmethod
def from_robot(cls, robot: "RobotClient", name: str) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/encoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]


Registry.register_subtype(
Registry.register_api(
ResourceRegistration(
Encoder,
EncoderRPCService,
Expand Down
4 changes: 2 additions & 2 deletions src/viam/components/encoder/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, Final, Optional, Tuple

from viam.proto.component.encoder import PositionType
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API

from ..component_base import ComponentBase

Expand All @@ -28,7 +28,7 @@ class Properties:
For more information, see `Encoder component <https://docs.viam.com/components/encoder/>`_.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "encoder"
)

Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/gantry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"Gantry",
]

Registry.register_subtype(ResourceRegistration(Gantry, GantryRPCService, lambda name, channel: GantryClient(name, channel)))
Registry.register_api(ResourceRegistration(Gantry, GantryRPCService, lambda name, channel: GantryClient(name, channel)))
4 changes: 2 additions & 2 deletions src/viam/components/gantry/gantry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
from typing import Any, Dict, Final, List, Optional

from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, Subtype
from viam.resource.types import RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, API

from ..component_base import ComponentBase

Expand All @@ -21,7 +21,7 @@ class Gantry(ComponentBase):
For more information, see `Gantry component <https://docs.viam.com/components/gantry/>`_.
"""

SUBTYPE: Final = Subtype( # pyright: ignore [reportIncompatibleVariableOverride]
API: Final = API( # pyright: ignore [reportIncompatibleVariableOverride]
RESOURCE_NAMESPACE_RDK, RESOURCE_TYPE_COMPONENT, "gantry"
)

Expand Down
2 changes: 1 addition & 1 deletion src/viam/components/generic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"Generic",
]

Registry.register_subtype(
Registry.register_api(
ResourceRegistration(
Generic,
GenericRPCService,
Expand Down
Loading
Loading