Skip to content

Commit 14c6348

Browse files
committed
misc: pre-commit / uv.lock
Explicitly declare protocol implementations
1 parent b1e4e1e commit 14c6348

File tree

13 files changed

+148
-155
lines changed

13 files changed

+148
-155
lines changed

src/plumpy/broadcast_filter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
# type: ignore
23
import re
34
import typing
45

src/plumpy/controller.py

Lines changed: 4 additions & 4 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
@@ -52,8 +53,7 @@ def play_process(self, pid: 'PID_TYPE') -> ProcessResult:
5253
...
5354

5455
def play_all(self) -> None:
55-
"""Play all processes that are subscribed to the same coordinator
56-
"""
56+
"""Play all processes that are subscribed to the same coordinator"""
5757

5858
def kill_process(self, pid: 'PID_TYPE', msg_text: str | None = None) -> Any:
5959
"""Kill the process

src/plumpy/coordinator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import annotations
33

4-
from typing import TYPE_CHECKING, Any, Callable, Hashable, Protocol
54
from re import Pattern
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,
@@ -36,7 +37,11 @@ def unhook_broadcast_receiver(self, identifier: 'ID_TYPE | None') -> None: ...
3637

3738
def unhook_task_receiver(self, identifier: 'ID_TYPE') -> None: ...
3839

39-
def rpc_send(self, recipient_id: Hashable, msg: Any,) -> Any: ...
40+
def rpc_send(
41+
self,
42+
recipient_id: Hashable,
43+
msg: Any,
44+
) -> Any: ...
4045

4146
def broadcast_send(
4247
self,

src/plumpy/message.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Union, cast
99

10-
from plumpy.coordinator import Coordinator
1110
from plumpy.exceptions import PersistenceError, TaskRejectedError
1211

1312
from . import loaders, persistence

src/plumpy/processes.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
cast,
3535
)
3636

37-
import kiwipy
38-
39-
from plumpy.broadcast_filter import BroadcastFilter
37+
from plumpy.broadcast_filter import BroadcastFilter # type: ignore
4038
from plumpy.coordinator import Coordinator
4139

4240
try:
@@ -944,19 +942,16 @@ def _fire_event(self, evt: Callable[..., Any], *args: Any, **kwargs: Any) -> Non
944942
# region Communication
945943

946944
def message_receive(self, msg: MessageType) -> Any:
947-
"""
948-
Coroutine called when the process receives a message from the communicator
945+
"""Coroutine called when the process receives a message from the communicator
949946
950-
:param _comm: the communicator that sent the message
951947
:param msg: the message
952948
:return: the outcome of processing the message, the return value will be sent back as a response to the sender
953949
"""
954-
# self.logger.debug(
955-
# "Process<%s>: received RPC message with communicator '%s': %r",
956-
# self.pid,
957-
# _comm,
958-
# msg,
959-
# )
950+
self.logger.debug(
951+
'Process<%s>: received RPC message: %r',
952+
self.pid,
953+
msg,
954+
)
960955

961956
intent = msg[message.INTENT_KEY]
962957

@@ -977,19 +972,17 @@ def message_receive(self, msg: MessageType) -> Any:
977972
def broadcast_receive(
978973
self, msg: MessageType, sender: Any, subject: Any, correlation_id: Any
979974
) -> Optional[concurrent.futures.Future]:
980-
"""
981-
Coroutine called when the process receives a message from the communicator
975+
"""Coroutine called when the process receives a message from the communicator
982976
983977
:param msg: the message
984978
"""
979+
self.logger.debug(
980+
"Process<%s>: received broadcast message '%s': %r",
981+
self.pid,
982+
subject,
983+
msg,
984+
)
985985

986-
# self.logger.debug(
987-
# "Process<%s>: received broadcast message '%s' with communicator '%s': %r",
988-
# self.pid,
989-
# subject,
990-
# _comm,
991-
# msg,
992-
# )
993986
# If we get a message we recognise then action it, otherwise ignore
994987
fn = None
995988
if subject == message.Intent.PLAY:

src/plumpy/rmq/communications.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def converted(communicator: kiwipy.Communicator, *args: Any, **kwargs: Any) -> k
8282
CommT = TypeVar('CommT', bound=kiwipy.Communicator)
8383

8484

85-
def wrap_communicator(communicator: CommT, loop: Optional[asyncio.AbstractEventLoop] = None) -> 'LoopCommunicator[CommT]':
85+
def wrap_communicator(
86+
communicator: CommT, loop: Optional[asyncio.AbstractEventLoop] = None
87+
) -> 'LoopCommunicator[CommT]':
8688
"""
8789
Wrap a communicator such that all callbacks made to any subscribers are scheduled on the
8890
given event loop.

src/plumpy/rmq/process_control.py

Lines changed: 4 additions & 4 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,8 +30,7 @@
2930
ProcessStatus = Any
3031

3132

32-
# FIXME: the class not fit typing of ProcessController protocol
33-
class RemoteProcessController:
33+
class RemoteProcessController(ProcessController):
3434
"""
3535
Control remote processes using coroutines that will send messages and wait
3636
(in a non-blocking way) for their response
@@ -190,7 +190,7 @@ async def execute_process(
190190
return result
191191

192192

193-
class RemoteProcessThreadController:
193+
class RemoteProcessThreadController(ProcessController):
194194
"""
195195
A class that can be used to control and launch remote processes
196196
"""
@@ -212,7 +212,7 @@ def get_status(self, pid: 'PID_TYPE') -> kiwipy.Future:
212212
"""
213213
return self._coordinator.rpc_send(pid, MessageBuilder.status())
214214

215-
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:
216216
"""Pause the process
217217
218218
: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

0 commit comments

Comments
 (0)