Skip to content

Commit fc3dc88

Browse files
committed
Explicitly declare protocol implementations
1 parent f698369 commit fc3dc88

File tree

8 files changed

+32
-10
lines changed

8 files changed

+32
-10
lines changed

src/plumpy/controller.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from __future__ import annotations
33

44
from collections.abc import Sequence
5-
from typing import Any, Hashable, Optional, Protocol, Union
5+
from typing import Any, Hashable, Optional, Protocol, Union, runtime_checkable
66

77
from plumpy import loaders
88
from plumpy.message import MessageType
@@ -12,6 +12,7 @@
1212
ProcessStatus = Any
1313

1414

15+
@runtime_checkable
1516
class ProcessController(Protocol):
1617
"""
1718
Control processes using coroutines that will send messages and wait
@@ -26,7 +27,7 @@ def get_status(self, pid: 'PID_TYPE') -> ProcessStatus:
2627
"""
2728
...
2829

29-
def pause_process(self, pid: 'PID_TYPE', msg: str | None = None) -> ProcessResult:
30+
def pause_process(self, pid: 'PID_TYPE', msg_text: str | None = None) -> Any:
3031
"""
3132
Pause the process
3233

src/plumpy/coordinator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from __future__ import annotations
33

44
from re import Pattern
5-
from typing import TYPE_CHECKING, Any, Callable, Hashable, Protocol
5+
from typing import TYPE_CHECKING, Any, Callable, Hashable, Protocol, runtime_checkable
66

77
if TYPE_CHECKING:
88
ID_TYPE = Hashable
99
Receiver = Callable[..., Any]
1010

1111

12+
@runtime_checkable
1213
class Coordinator(Protocol):
1314
def hook_rpc_receiver(
1415
self,

src/plumpy/rmq/process_control.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import kiwipy
1010

1111
from plumpy import loaders
12+
from plumpy.controller import ProcessController
1213
from plumpy.coordinator import Coordinator
1314
from plumpy.message import (
1415
Intent,
@@ -29,9 +30,7 @@
2930
ProcessStatus = Any
3031

3132

32-
# This class not conform with typing of ProcessController protocol.
33-
# Does't matter too much, since this controller is not directly used as the controller by downstream.
34-
class RemoteProcessController:
33+
class RemoteProcessController(ProcessController):
3534
"""
3635
Control remote processes using coroutines that will send messages and wait
3736
(in a non-blocking way) for their response
@@ -191,7 +190,7 @@ async def execute_process(
191190
return result
192191

193192

194-
class RemoteProcessThreadController:
193+
class RemoteProcessThreadController(ProcessController):
195194
"""
196195
A class that can be used to control and launch remote processes
197196
"""
@@ -213,7 +212,7 @@ def get_status(self, pid: 'PID_TYPE') -> kiwipy.Future:
213212
"""
214213
return self._coordinator.rpc_send(pid, MessageBuilder.status())
215214

216-
def pause_process(self, pid: 'PID_TYPE', msg_text: str | None = None) -> kiwipy.Future:
215+
def pause_process(self, pid: 'PID_TYPE', msg_text: str | None = None) -> Any:
217216
"""Pause the process
218217
219218
:param pid: the pid of the process to pause

tests/rmq/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import kiwipy
66
import concurrent.futures
77

8+
from plumpy.coordinator import Coordinator
89
from plumpy.exceptions import CoordinatorConnectionError
910

1011
if TYPE_CHECKING:
@@ -15,7 +16,7 @@
1516

1617

1718
@final
18-
class RmqCoordinator(Generic[U]):
19+
class RmqCoordinator(Coordinator, Generic[U]):
1920
def __init__(self, comm: U):
2021
self._comm = comm
2122

tests/rmq/test_coordinator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
from plumpy.coordinator import Coordinator
3+
from . import RmqCoordinator
4+
5+
6+
def test_mock_coordinator():
7+
assert isinstance(RmqCoordinator, Coordinator)

tests/rmq/test_process_control.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from kiwipy import rmq
88

99
import plumpy
10+
from plumpy.controller import ProcessController
1011
from plumpy.rmq import process_control
1112

1213
from . import RmqCoordinator
@@ -42,6 +43,10 @@ def async_controller(_coordinator):
4243
def sync_controller(_coordinator):
4344
yield process_control.RemoteProcessThreadController(_coordinator)
4445

46+
def test_remote_process_controller(sync_controller, async_controller):
47+
assert isinstance(sync_controller, ProcessController)
48+
assert isinstance(async_controller, ProcessController)
49+
4550

4651
class TestRemoteProcessController:
4752
@pytest.mark.asyncio

tests/test_coordinator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
from plumpy.coordinator import Coordinator
3+
from .utils import MockCoordinator
4+
5+
6+
def test_mock_coordinator():
7+
assert isinstance(MockCoordinator, Coordinator)

tests/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import plumpy
1515
from plumpy import persistence, process_states, processes, utils
16+
from plumpy.coordinator import Coordinator
1617
from plumpy.exceptions import CoordinatorConnectionError
1718
from plumpy.message import MessageBuilder
1819
from plumpy.rmq import TaskRejected
@@ -25,7 +26,7 @@
2526
Snapshot = collections.namedtuple('Snapshot', ['state', 'bundle', 'outputs'])
2627

2728

28-
class MockCoordinator:
29+
class MockCoordinator(Coordinator):
2930
def __init__(self):
3031
self._task_receivers = {}
3132
self._broadcast_receivers = {}

0 commit comments

Comments
 (0)