From 562a52a7e1fe0060e09ce9a829bb2e1d66ea8c45 Mon Sep 17 00:00:00 2001 From: Alan Davidson Date: Mon, 11 Dec 2023 17:20:17 -0500 Subject: [PATCH 1/5] remove impossible-to-implement functions from digital interrupts --- examples/server/v1/components.py | 15 ------------- src/viam/components/board/board.py | 35 ----------------------------- src/viam/components/board/client.py | 9 -------- tests/mocks/components.py | 13 +---------- 4 files changed, 1 insertion(+), 71 deletions(-) diff --git a/examples/server/v1/components.py b/examples/server/v1/components.py index a406769e4..4670364b7 100644 --- a/examples/server/v1/components.py +++ b/examples/server/v1/components.py @@ -241,27 +241,12 @@ async def read(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> int: class ExampleDigitalInterrupt(Board.DigitalInterrupt): def __init__(self, name: str): - self.high = False - self.last_tick = 0 self.num_ticks = 0 - self.callbacks: List[Queue] = [] - self.post_processors: List[PostProcessor] = [] super().__init__(name) async def value(self, extra: Optional[Dict[str, Any]] = None, **kwargs) -> int: return self.num_ticks - async def tick(self, high: bool, nanos: int): - self.high = high - self.last_tick = nanos - self.num_ticks += 1 - - async def add_callback(self, queue: Queue): - self.callbacks.append(queue) - - async def add_post_processor(self, processor: PostProcessor): - self.post_processors.append(processor) - class ExampleGPIOPin(Board.GPIOPin): def __init__(self, name: str): diff --git a/src/viam/components/board/board.py b/src/viam/components/board/board.py index 7f71c6e7e..ab84ece22 100644 --- a/src/viam/components/board/board.py +++ b/src/viam/components/board/board.py @@ -57,41 +57,6 @@ async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Option """ ... - @abc.abstractmethod - async def tick(self, high: bool, nanos: int): - """ - This method is to be called either manually if the interrupt - is a proxy to some real hardware interrupt or for tests. - - Args: - high (bool): If the signal of the interrupt is high. - nanos (int): Nanoseconds from an arbitrary point in time, - but always increasing and always needs to be accurate. - Using ``time.time_ns()`` would be acceptable. - """ - ... - - @abc.abstractmethod - async def add_callback(self, queue: Queue): - """ - Add a callback to be sent the low/high value on ``tick()``. - - Args: - queue (Queue): The receiving queue. - """ - ... - - @abc.abstractmethod - async def add_post_processor(self, processor: PostProcessor): - """ - Add a post processor that should be used to modify what - is returned by ``self.value()`` - - Args: - processor (PostProcessor): The post processor to add. - """ - ... - class GPIOPin(ComponentBase): """ Abstract representation of an individual GPIO pin on a board diff --git a/src/viam/components/board/client.py b/src/viam/components/board/client.py index a7f1dc5d9..91b87ac5c 100644 --- a/src/viam/components/board/client.py +++ b/src/viam/components/board/client.py @@ -59,15 +59,6 @@ async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Option response: GetDigitalInterruptValueResponse = await self.board.client.GetDigitalInterruptValue(request, timeout=timeout) return response.value - async def tick(self, high: bool, nanos: int): - raise NotImplementedError() - - async def add_callback(self, queue: Queue): - raise NotImplementedError() - - async def add_post_processor(self, processor: PostProcessor): - raise NotImplementedError() - class GPIOPinClient(Board.GPIOPin): def __init__(self, name: str, board: "BoardClient"): diff --git a/tests/mocks/components.py b/tests/mocks/components.py index 89f6b7e86..44d0a4323 100644 --- a/tests/mocks/components.py +++ b/tests/mocks/components.py @@ -262,27 +262,16 @@ def __init__(self, name: str): self.high = False self.last_tick = 0 self.num_ticks = 0 - self.callbacks: List[Queue] = [] - self.post_processors: List[PostProcessor] = [] - self.timeout: Optional[float] = None super().__init__(name) async def value(self, *, extra: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None, **kwargs) -> int: - self.extra = extra - self.timeout = timeout return self.num_ticks - async def tick(self, high: bool, nanos: int): + async def tick(self, high: bool, nanos: int): # Call this to get the mock interrupt to change self.high = high self.last_tick = nanos self.num_ticks += 1 - async def add_callback(self, queue: Queue): - self.callbacks.append(queue) - - async def add_post_processor(self, processor: PostProcessor): - self.post_processors.append(processor) - class MockGPIOPin(Board.GPIOPin): def __init__(self, name: str): From 1d3e94e005ae547f96ae1fbf2126400abdce978b Mon Sep 17 00:00:00 2001 From: Alan Davidson Date: Mon, 11 Dec 2023 17:41:37 -0500 Subject: [PATCH 2/5] Remove PostProcessor as dead code, too --- examples/server/v1/components.py | 1 - src/viam/components/board/board.py | 2 -- src/viam/components/board/client.py | 1 - tests/mocks/components.py | 1 - 4 files changed, 5 deletions(-) diff --git a/examples/server/v1/components.py b/examples/server/v1/components.py index 4670364b7..5bbdb7b2a 100644 --- a/examples/server/v1/components.py +++ b/examples/server/v1/components.py @@ -20,7 +20,6 @@ from viam.components.audio_input import AudioInput from viam.components.base import Base from viam.components.board import Board -from viam.components.board.board import PostProcessor from viam.components.camera import Camera from viam.components.encoder import Encoder from viam.components.gantry import Gantry diff --git a/src/viam/components/board/board.py b/src/viam/components/board/board.py index ab84ece22..406bcc023 100644 --- a/src/viam/components/board/board.py +++ b/src/viam/components/board/board.py @@ -9,8 +9,6 @@ from ..component_base import ComponentBase -PostProcessor = Callable[[int], int] - class Board(ComponentBase): """ diff --git a/src/viam/components/board/client.py b/src/viam/components/board/client.py index 91b87ac5c..e8bb3fad2 100644 --- a/src/viam/components/board/client.py +++ b/src/viam/components/board/client.py @@ -31,7 +31,6 @@ from viam.utils import ValueTypes, dict_to_struct, get_geometries, struct_to_dict from . import Board -from .board import PostProcessor class AnalogReaderClient(Board.AnalogReader): diff --git a/tests/mocks/components.py b/tests/mocks/components.py index 44d0a4323..144160d93 100644 --- a/tests/mocks/components.py +++ b/tests/mocks/components.py @@ -18,7 +18,6 @@ from viam.components.audio_input import AudioInput from viam.components.base import Base from viam.components.board import Board -from viam.components.board.board import PostProcessor from viam.components.camera import Camera, DistortionParameters, IntrinsicParameters from viam.components.encoder import Encoder from viam.components.gantry import Gantry From 22039c34ea6ff216912ad0167131d28285ed6b07 Mon Sep 17 00:00:00 2001 From: Alan Davidson Date: Mon, 11 Dec 2023 17:44:47 -0500 Subject: [PATCH 3/5] remove unused imports --- examples/server/v1/components.py | 2 +- src/viam/components/board/board.py | 3 +-- src/viam/components/board/client.py | 1 - tests/mocks/components.py | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/server/v1/components.py b/examples/server/v1/components.py index 5bbdb7b2a..13c73892d 100644 --- a/examples/server/v1/components.py +++ b/examples/server/v1/components.py @@ -10,7 +10,7 @@ from typing import AsyncIterator from datetime import timedelta -from multiprocessing import Lock, Queue +from multiprocessing import Lock from pathlib import Path from typing import Any, Dict, List, Mapping, Optional, Tuple diff --git a/src/viam/components/board/board.py b/src/viam/components/board/board.py index 406bcc023..8fee79133 100644 --- a/src/viam/components/board/board.py +++ b/src/viam/components/board/board.py @@ -1,7 +1,6 @@ import abc from datetime import timedelta -from multiprocessing import Queue -from typing import Any, Callable, Dict, Final, List, Optional +from typing import Any, Dict, Final, List, Optional from viam.proto.common import BoardStatus from viam.proto.component.board import PowerMode diff --git a/src/viam/components/board/client.py b/src/viam/components/board/client.py index e8bb3fad2..7a88c99dc 100644 --- a/src/viam/components/board/client.py +++ b/src/viam/components/board/client.py @@ -1,5 +1,4 @@ from datetime import timedelta -from multiprocessing import Queue from typing import Any, Dict, List, Mapping, Optional from google.protobuf.duration_pb2 import Duration diff --git a/tests/mocks/components.py b/tests/mocks/components.py index 144160d93..83099b0d2 100644 --- a/tests/mocks/components.py +++ b/tests/mocks/components.py @@ -7,7 +7,6 @@ from dataclasses import dataclass from datetime import datetime, timedelta -from multiprocessing import Queue from secrets import choice from typing import Any, Dict, List, Mapping, Optional, Tuple, Union From 1d7c07c6d4e1ad306a07eda66df9ab9244bee369 Mon Sep 17 00:00:00 2001 From: Alan Davidson Date: Mon, 11 Dec 2023 17:53:14 -0500 Subject: [PATCH 4/5] don't assert that anything is in the extra field; that is by default unused --- tests/test_board.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_board.py b/tests/test_board.py index 0c4ebe7d7..31795162f 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -179,15 +179,13 @@ async def test_get_digital_interrupt_value(self, board: MockBoard, service: Boar request = GetDigitalInterruptValueRequest(board_name=board.name, digital_interrupt_name="dne") await client.GetDigitalInterruptValue(request) - extra = {"foo": "bar", "baz": [1, 2, 3]} request = GetDigitalInterruptValueRequest( - board_name=board.name, digital_interrupt_name="interrupt1", extra=dict_to_struct(extra) + board_name=board.name, digital_interrupt_name="interrupt1" ) response: GetDigitalInterruptValueResponse = await client.GetDigitalInterruptValue(request, timeout=18.2) assert response.value == 0 interrupt = cast(MockDigitalInterrupt, board.digital_interrupts["interrupt1"]) - assert interrupt.extra == extra assert interrupt.timeout == loose_approx(18.2) @pytest.mark.asyncio From 5ac625fa207998b18231098f8635df4938d2a655 Mon Sep 17 00:00:00 2001 From: Alan Davidson Date: Mon, 11 Dec 2023 18:04:47 -0500 Subject: [PATCH 5/5] don't test the nonexistent parts of digital interrupts --- tests/test_board.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_board.py b/tests/test_board.py index 31795162f..4e90f7f90 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -175,19 +175,18 @@ async def test_get_digital_interrupt_value(self, board: MockBoard, service: Boar async with ChannelFor([service]) as channel: client = BoardServiceStub(channel) + request = GetDigitalInterruptValueRequest( + board_name=board.name, digital_interrupt_name="dne" + ) with pytest.raises(GRPCError, match=r".*Status.NOT_FOUND.*"): - request = GetDigitalInterruptValueRequest(board_name=board.name, digital_interrupt_name="dne") await client.GetDigitalInterruptValue(request) request = GetDigitalInterruptValueRequest( board_name=board.name, digital_interrupt_name="interrupt1" ) - response: GetDigitalInterruptValueResponse = await client.GetDigitalInterruptValue(request, timeout=18.2) + response: GetDigitalInterruptValueResponse = await client.GetDigitalInterruptValue(request) assert response.value == 0 - interrupt = cast(MockDigitalInterrupt, board.digital_interrupts["interrupt1"]) - assert interrupt.timeout == loose_approx(18.2) - @pytest.mark.asyncio async def test_set_gpio(self, board: MockBoard, service: BoardRPCService): async with ChannelFor([service]) as channel: