Skip to content

Commit

Permalink
Add cast of message type in command interpreter (#75)
Browse files Browse the repository at this point in the history
* #74 Add cast of message type in command interpreter

Co-authored-by: Garry O'Donnell <garry.o'donnell@diamond.ac.uk>

* Add type hint patch to command interpreter tests

Co-authored-by: Callum Forrester <29771545+callumforrester@users.noreply.github.com>

Co-authored-by: Garry O'Donnell <garry.o'donnell@diamond.ac.uk>
Co-authored-by: Callum Forrester <29771545+callumforrester@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 3, 2022
1 parent db5cde0 commit 3dbfeb9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 21 additions & 1 deletion tests/adapters/interpreters/command/test_command_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
from typing import Callable, Optional, Sequence

import pytest
from mock import AsyncMock, MagicMock
from mock import AsyncMock, MagicMock, patch

from tickit.adapters.interpreters.command import CommandInterpreter
from tickit.adapters.interpreters.command.command_interpreter import Command
from tickit.core.adapter import Adapter

_GET_TYPE_HINTS = (
"tickit.adapters.interpreters.command.command_interpreter.get_type_hints"
)


@pytest.fixture
def command_interpreter():
Expand All @@ -33,6 +37,10 @@ def parse(self, data: bytes) -> Optional[Sequence[str]]:
return TestCommand


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_calls_func_with_args(
command_interpreter: CommandInterpreter,
Expand All @@ -51,6 +59,10 @@ async def test_command_interpreter_handle_calls_func_with_args(
test_adapter.test_method.assert_awaited_once_with("arg1", "arg2")


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_returns_iterable_reply(
command_interpreter: CommandInterpreter,
Expand All @@ -70,6 +82,10 @@ async def test_command_interpreter_handle_returns_iterable_reply(
assert reply == (await command_interpreter.handle(test_adapter, b"\x01"))[0]


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_wraps_non_iterable_reply(
command_interpreter: CommandInterpreter,
Expand All @@ -94,6 +110,10 @@ async def test_command_interpreter_handle_wraps_non_iterable_reply(
)


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
@pytest.mark.parametrize("interrupt", [True, False])
async def test_command_interpreter_handle_returns_interupt(
Expand Down
6 changes: 5 additions & 1 deletion tickit/adapters/interpreters/command/command_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod
from inspect import getmembers
from typing import AnyStr, AsyncIterable, Optional, Sequence, Tuple
from typing import AnyStr, AsyncIterable, Optional, Sequence, Tuple, get_type_hints

from tickit.core.adapter import Adapter, Interpreter
from tickit.utils.compat.typing_compat import Protocol, runtime_checkable
Expand Down Expand Up @@ -90,6 +90,10 @@ async def handle(
args = command.parse(message)
if args is None:
continue
args = (
argtype(arg)
for arg, argtype in zip(args, get_type_hints(method).values())
)
resp = await method(*args)
if not isinstance(resp, AsyncIterable):
resp = CommandInterpreter._wrap(resp)
Expand Down

0 comments on commit 3dbfeb9

Please sign in to comment.