From 194b1eca614e53e11e28029685e1fc90c72fea9d Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Mon, 4 Dec 2023 16:07:03 +0000 Subject: [PATCH 1/5] typing overrides --- spinnman/spalloc/spalloc_client.py | 25 +++++++++++++------- spinnman/spalloc/spalloc_eieio_connection.py | 2 +- spinnman/spalloc/spalloc_eieio_listener.py | 2 +- spinnman/spalloc/spalloc_job.py | 4 ++-- spinnman/spalloc/spalloc_scp_connection.py | 7 +++--- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index 462cb0963..50768f206 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -212,8 +212,11 @@ def create_job_rect( @overrides(AbstractSpallocClient.create_job_board) def create_job_board( - self, triad=None, physical=None, ip_address=None, - machine_name=None, keepalive=45) -> SpallocJob: + self, triad: Optional[Tuple[int, int, int]] = None, + physical: Optional[Tuple[int, int, int]] = None, + ip_address: Optional[str] = None, + machine_name: Optional[str] = None, + keepalive: int = 45) -> SpallocJob: board: JsonObject if triad: x, y, z = triad @@ -233,8 +236,12 @@ def create_job_board( @overrides(AbstractSpallocClient.create_job_rect_at_board) def create_job_rect_at_board( - self, width, height, triad=None, physical=None, ip_address=None, - machine_name=None, keepalive=45, max_dead_boards=0): + self, width: int, height: int, + triad: Optional[Tuple[int, int, int]] = None, + physical: Optional[Tuple[int, int, int]] = None, + ip_address: Optional[str] = None, + machine_name: Optional[str] = None, keepalive: int = 45, + max_dead_boards: int = 0) -> SpallocJob: if triad: x, y, z = triad board = {"x": int(x), "y": int(y), "z": int(z)} @@ -510,7 +517,7 @@ def __init__(self, session: Session, job_handle: str): self.__proxy_ping: Optional[_ProxyPing] = None @overrides(SpallocJob.get_session_credentials_for_db) - def get_session_credentials_for_db(self): + def get_session_credentials_for_db(self) -> Mapping[Tuple[str, str], str]: config = {} config["SPALLOC", "service uri"] = self._service_url config["SPALLOC", "job uri"] = self._url @@ -634,7 +641,7 @@ def wait_until_ready(self, timeout: Optional[int] = None, raise SpallocException("job was unexpectedly destroyed") @overrides(SpallocJob.destroy) - def destroy(self, reason: str = "finished") -> None: + def destroy(self, reason: str = "finished"): if self.__keepalive_handle: self.__keepalive_handle.close() self.__keepalive_handle = None @@ -871,7 +878,7 @@ def __init__( super().__init__(ws, receiver) @overrides(_ProxiedConnection._open_connection) - def _open_connection(self): + def _open_connection(self) -> int: handle, = self._call( ProxyProtocol.OPEN, _open_req, _open_close_res, *self.__connect_args) @@ -882,7 +889,7 @@ def is_connected(self) -> bool: return self._connected @overrides(Connection.close) - def close(self): + def close(self) -> None: self._close() @overrides(SpallocProxiedConnection.send) @@ -941,7 +948,7 @@ def close(self) -> None: self._close() @overrides(SpallocProxiedConnection.send) - def send(self, data: bytes) -> None: + def send(self, data: bytes): self._throw_if_closed() raise IOError("socket is not open for sending") diff --git a/spinnman/spalloc/spalloc_eieio_connection.py b/spinnman/spalloc/spalloc_eieio_connection.py index 613c7c7db..1a8c1c6b9 100644 --- a/spinnman/spalloc/spalloc_eieio_connection.py +++ b/spinnman/spalloc/spalloc_eieio_connection.py @@ -45,7 +45,7 @@ class SpallocEIEIOConnection( __slots__ = () @overrides(EIEIOConnection.send_eieio_message) - def send_eieio_message(self, eieio_message): + def send_eieio_message(self, eieio_message: AbstractEIEIOMessage): # Not normally used, as packets need headers to go to SpiNNaker self.send(eieio_message.bytestring) diff --git a/spinnman/spalloc/spalloc_eieio_listener.py b/spinnman/spalloc/spalloc_eieio_listener.py index 435fba604..9e7e2b94a 100644 --- a/spinnman/spalloc/spalloc_eieio_listener.py +++ b/spinnman/spalloc/spalloc_eieio_listener.py @@ -57,7 +57,7 @@ def receive_eieio_message( return read_eieio_data_message(data, 0) @overrides(SpallocProxiedConnection.send) - def send(self, data): + def send(self, data: bytes): """ .. note:: This class does not allow sending. diff --git a/spinnman/spalloc/spalloc_job.py b/spinnman/spalloc/spalloc_job.py index 7cc8caac4..adb7b2b31 100644 --- a/spinnman/spalloc/spalloc_job.py +++ b/spinnman/spalloc/spalloc_job.py @@ -34,7 +34,7 @@ class SpallocJob(object, metaclass=AbstractBase): __slots__ = () @abstractmethod - def get_state(self, wait_for_change=False) -> SpallocState: + def get_state(self, wait_for_change: bool = False) -> SpallocState: """ Get the current state of the machine. @@ -183,7 +183,7 @@ def destroy(self, reason: str = "finished"): raise NotImplementedError() @abstractmethod - def keepalive(self): + def keepalive(self) -> None: """ Signal the job that we want it to stay alive for a while longer. """ diff --git a/spinnman/spalloc/spalloc_scp_connection.py b/spinnman/spalloc/spalloc_scp_connection.py index a88c140fc..bfd42d377 100644 --- a/spinnman/spalloc/spalloc_scp_connection.py +++ b/spinnman/spalloc/spalloc_scp_connection.py @@ -55,8 +55,8 @@ def send_sdp_message(self, sdp_message: SDPMessage): self.send(_TWO_SKIP + sdp_message.bytestring) @overrides(SCAMPConnection.receive_scp_response) - def receive_scp_response( - self, timeout=1.0) -> Tuple[SCPResult, int, bytes, int]: + def receive_scp_response(self, timeout: Optional[float] = 1.0) -> Tuple[ + SCPResult, int, bytes, int]: data = self.receive(timeout) result, sequence = _TWO_SHORTS.unpack_from(data, 10) return SCPResult(result), sequence, data, 2 @@ -67,7 +67,8 @@ def send_scp_request(self, scp_request: AbstractSCPRequest): @overrides(SCAMPConnection.get_scp_data) def get_scp_data( - self, scp_request: AbstractSCPRequest, x=None, y=None) -> bytes: + self, scp_request: AbstractSCPRequest, + x: Optional[int] = None, y: Optional[int] = None) -> bytes: if x is None: x = self.chip_x if y is None: From fc3e7269a95feadfa6fe2c2fe1d8419150927d59 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Fri, 8 Dec 2023 11:51:22 +0000 Subject: [PATCH 2/5] more typing of overide methods --- .../connections/udp_packet_connections/bmp_connection.py | 4 ++-- .../eieio/command_messages/eieio_command_message.py | 7 ++++--- spinnman/messages/scp/impl/check_ok_response.py | 2 +- spinnman/messages/scp/impl/sdram_alloc.py | 2 +- spinnman/processes/get_exclude_cpu_info_process.py | 2 +- spinnman/processes/get_include_cpu_info_process.py | 2 +- spinnman/transceiver/base_transceiver.py | 6 +++--- spinnman/transceiver/mockable_transceiver.py | 9 +++++---- spinnman/transceiver/transceiver.py | 7 ++++--- 9 files changed, 22 insertions(+), 19 deletions(-) diff --git a/spinnman/connections/udp_packet_connections/bmp_connection.py b/spinnman/connections/udp_packet_connections/bmp_connection.py index 63e45379e..584c4129c 100644 --- a/spinnman/connections/udp_packet_connections/bmp_connection.py +++ b/spinnman/connections/udp_packet_connections/bmp_connection.py @@ -13,7 +13,7 @@ # limitations under the License. import struct -from typing import Sequence, Tuple +from typing import Optional, Sequence, Tuple from spinn_utilities.overrides import overrides from .udp_connection import UDPConnection from spinnman.constants import SCP_SCAMP_PORT @@ -74,7 +74,7 @@ def get_scp_data(self, scp_request: AbstractSCPRequest) -> bytes: return _TWO_SKIP.pack() + scp_request.bytestring @overrides(AbstractSCPConnection.receive_scp_response) - def receive_scp_response(self, timeout=1.0) -> Tuple[ + def receive_scp_response(self, timeout: Optional[float] = 1.0) -> Tuple[ SCPResult, int, bytes, int]: data = self.receive(timeout) result, sequence = _TWO_SHORTS.unpack_from(data, 10) diff --git a/spinnman/messages/eieio/command_messages/eieio_command_message.py b/spinnman/messages/eieio/command_messages/eieio_command_message.py index 906af845c..accdfe400 100644 --- a/spinnman/messages/eieio/command_messages/eieio_command_message.py +++ b/spinnman/messages/eieio/command_messages/eieio_command_message.py @@ -13,6 +13,7 @@ # limitations under the License. from spinn_utilities.overrides import overrides from spinnman.messages.eieio import AbstractEIEIOMessage +from spinnman.messages.eieio.command_messages import EIEIOCommandHeader class EIEIOCommandMessage(AbstractEIEIOMessage): @@ -24,7 +25,7 @@ class EIEIOCommandMessage(AbstractEIEIOMessage): "_eieio_command_header", "_offset") - def __init__(self, eieio_command_header, data=None, offset=0): + def __init__(self, eieio_command_header: EIEIOCommandHeader, data=None, offset=0): """ :param EIEIOCommandHeader eieio_command_header: The header of the message @@ -40,7 +41,7 @@ def __init__(self, eieio_command_header, data=None, offset=0): @property @overrides(AbstractEIEIOMessage.eieio_header) - def eieio_header(self): + def eieio_header(self) -> EIEIOCommandHeader: """ :rtype: EIEIOCommandHeader """ @@ -60,7 +61,7 @@ def from_bytestring(command_header, data, offset): @property @overrides(AbstractEIEIOMessage.bytestring) - def bytestring(self): + def bytestring(self) -> bytes: return self._eieio_command_header.bytestring @staticmethod diff --git a/spinnman/messages/scp/impl/check_ok_response.py b/spinnman/messages/scp/impl/check_ok_response.py index 662ea9d9b..4f4a140b0 100644 --- a/spinnman/messages/scp/impl/check_ok_response.py +++ b/spinnman/messages/scp/impl/check_ok_response.py @@ -37,7 +37,7 @@ def __init__(self, operation: str, command): self._command = command @overrides(AbstractSCPResponse.read_data_bytestring) - def read_data_bytestring(self, data: bytes, offset: int) -> None: + def read_data_bytestring(self, data: bytes, offset: int): result = self.scp_response_header.result if result != SCPResult.RC_OK: raise SpinnmanUnexpectedResponseCodeException( diff --git a/spinnman/messages/scp/impl/sdram_alloc.py b/spinnman/messages/scp/impl/sdram_alloc.py index 30821d2f5..ad904146b 100644 --- a/spinnman/messages/scp/impl/sdram_alloc.py +++ b/spinnman/messages/scp/impl/sdram_alloc.py @@ -42,7 +42,7 @@ def __init__(self, size: int): self._base_address: Optional[int] = None @overrides(AbstractSCPResponse.read_data_bytestring) - def read_data_bytestring(self, data, offset): + def read_data_bytestring(self, data: bytes, offset: int): result = self.scp_response_header.result if result != SCPResult.RC_OK: raise SpinnmanUnexpectedResponseCodeException( diff --git a/spinnman/processes/get_exclude_cpu_info_process.py b/spinnman/processes/get_exclude_cpu_info_process.py index 7a5bbbbca..ca5bca04e 100644 --- a/spinnman/processes/get_exclude_cpu_info_process.py +++ b/spinnman/processes/get_exclude_cpu_info_process.py @@ -30,5 +30,5 @@ def __init__(self, connection_selector: ConnectionSelector, self.__states = states @overrides(GetCPUInfoProcess._is_desired) - def _is_desired(self, cpu_info: CPUInfo): + def _is_desired(self, cpu_info: CPUInfo) -> bool: return cpu_info.state not in self.__states diff --git a/spinnman/processes/get_include_cpu_info_process.py b/spinnman/processes/get_include_cpu_info_process.py index 2fd1154c4..749549983 100644 --- a/spinnman/processes/get_include_cpu_info_process.py +++ b/spinnman/processes/get_include_cpu_info_process.py @@ -35,5 +35,5 @@ def __init__(self, connection_selector: ConnectionSelector, self.__states = states @overrides(GetCPUInfoProcess._is_desired) - def _is_desired(self, cpu_info: CPUInfo): + def _is_desired(self, cpu_info: CPUInfo) -> bool: return cpu_info.state in self.__states diff --git a/spinnman/transceiver/base_transceiver.py b/spinnman/transceiver/base_transceiver.py index 65e4139e6..bf2a20acc 100644 --- a/spinnman/transceiver/base_transceiver.py +++ b/spinnman/transceiver/base_transceiver.py @@ -25,7 +25,7 @@ import time from typing import ( BinaryIO, Collection, Dict, FrozenSet, Iterable, Iterator, List, Optional, - Sequence, Tuple, TypeVar, Union, cast) + Sequence, Set, Tuple, TypeVar, Union, cast) from spinn_utilities.abstract_base import ( AbstractBase, abstractmethod) from spinn_utilities.config_holder import get_config_bool @@ -422,7 +422,7 @@ def add_scamp_connections(self, connections: Dict[XY, str]): self._scamp_connections) @overrides(Transceiver.get_connections) - def get_connections(self): + def get_connections(self) -> Set[Connection]: return self._all_connections def _get_machine_dimensions(self) -> MachineDimensions: @@ -1232,7 +1232,7 @@ def get_tags(self, connection: Optional[SCAMPConnection] = None @overrides(Transceiver.malloc_sdram) def malloc_sdram( - self, x: int, y: int, size: int, app_id: int, tag=0) -> int: + self, x: int, y: int, size: int, app_id: int, tag: int = 0) -> int: try: process = MallocSDRAMProcess(self._scamp_connection_selector) process.malloc_sdram(x, y, size, app_id, tag) diff --git a/spinnman/transceiver/mockable_transceiver.py b/spinnman/transceiver/mockable_transceiver.py index 558cc7505..126ee9732 100644 --- a/spinnman/transceiver/mockable_transceiver.py +++ b/spinnman/transceiver/mockable_transceiver.py @@ -16,7 +16,7 @@ from typing import ( BinaryIO, Collection, Dict, FrozenSet, Iterable, - List, Optional, Tuple, Union) + List, Optional, Set, Tuple, Union) from spinn_utilities.overrides import overrides from spinn_utilities.progress_bar import ProgressBar from spinn_utilities.typing.coords import XY @@ -24,6 +24,7 @@ CoreSubsets, FixedRouteEntry, Machine, MulticastRoutingEntry) from spinn_machine.tags import AbstractTag, IPTag, ReverseIPTag from spinnman.data import SpiNNManDataView +from spinnman.connections.abstract_classes import Connection from spinnman.connections.udp_packet_connections import BMPConnection from spinnman.connections.udp_packet_connections import ( SCAMPConnection, SDPConnection) @@ -69,7 +70,7 @@ def get_machine_details(self) -> Machine: return SpiNNManDataView.get_machine() @overrides(Transceiver.get_connections) - def get_connections(self): + def get_connections(self) -> Set[Connection]: raise NotImplementedError("Needs to be mocked") @overrides(Transceiver.get_cpu_infos) @@ -80,7 +81,7 @@ def get_cpu_infos( raise NotImplementedError("Needs to be mocked") @overrides(Transceiver.get_clock_drift) - def get_clock_drift(self, x, y): + def get_clock_drift(self, x: int, y: int) -> float: raise NotImplementedError("Needs to be mocked") @overrides(Transceiver.read_user) @@ -196,7 +197,7 @@ def get_tags(self, connection: Optional[SCAMPConnection] = None @overrides(Transceiver.malloc_sdram) def malloc_sdram( - self, x: int, y: int, size: int, app_id: int, tag=0) -> int: + self, x: int, y: int, size: int, app_id: int, tag: int = 0) -> int: raise NotImplementedError("Needs to be mocked") @overrides(Transceiver.load_multicast_routes) diff --git a/spinnman/transceiver/transceiver.py b/spinnman/transceiver/transceiver.py index faa361054..d33103274 100644 --- a/spinnman/transceiver/transceiver.py +++ b/spinnman/transceiver/transceiver.py @@ -16,13 +16,14 @@ from typing import ( BinaryIO, Collection, Dict, FrozenSet, Iterable, - List, Optional, Tuple, Union) + List, Optional, Set, Tuple, Union) from spinn_utilities.abstract_base import abstractmethod from spinn_utilities.progress_bar import ProgressBar from spinn_utilities.typing.coords import XY from spinn_machine import ( CoreSubsets, FixedRouteEntry, Machine, MulticastRoutingEntry) from spinn_machine.tags import AbstractTag, IPTag, ReverseIPTag +from spinnman.connections.abstract_classes import Connection from spinnman.connections.udp_packet_connections import ( SCAMPConnection, SDPConnection) from spinnman.messages.scp.enums import Signal @@ -111,7 +112,7 @@ def add_scamp_connections(self, connections: Dict[XY, str]): raise NotImplementedError("abstractmethod") @abstractmethod - def get_connections(self): + def get_connections(self) -> Set[Connection]: """ Get the currently known connections to the board, made up of those passed in to the transceiver and those that are discovered during @@ -732,7 +733,7 @@ def get_tags(self, connection: Optional[SCAMPConnection] = None @abstractmethod def malloc_sdram( - self, x: int, y: int, size: int, app_id: int, tag=0) -> int: + self, x: int, y: int, size: int, app_id: int, tag: int = 0) -> int: """ Allocates a chunk of SDRAM on a chip on the machine. From edb7332ec74d581b63d223158a1b2b4da8177007 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 19 Dec 2023 09:02:07 +0000 Subject: [PATCH 3/5] flake8 --- .../messages/eieio/command_messages/eieio_command_message.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spinnman/messages/eieio/command_messages/eieio_command_message.py b/spinnman/messages/eieio/command_messages/eieio_command_message.py index accdfe400..f28002eb5 100644 --- a/spinnman/messages/eieio/command_messages/eieio_command_message.py +++ b/spinnman/messages/eieio/command_messages/eieio_command_message.py @@ -25,7 +25,8 @@ class EIEIOCommandMessage(AbstractEIEIOMessage): "_eieio_command_header", "_offset") - def __init__(self, eieio_command_header: EIEIOCommandHeader, data=None, offset=0): + def __init__(self, eieio_command_header: EIEIOCommandHeader, data=None, + offset=0): """ :param EIEIOCommandHeader eieio_command_header: The header of the message From 9f17703735cf527f3bebe2a7c756f0fa484663af Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 19 Dec 2023 13:07:17 +0000 Subject: [PATCH 4/5] typing --- spinnman/spalloc/spalloc_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index 50768f206..2df61eb79 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -28,7 +28,7 @@ from typing import ( Any, ContextManager, Callable, Dict, FrozenSet, Iterable, Iterator, List, Mapping, - Optional, Tuple, cast) + Optional, Tuple, Union, cast) from typing_extensions import TypeAlias from websocket import WebSocket # type: ignore from spinn_utilities.abstract_base import AbstractBase, abstractmethod @@ -242,6 +242,7 @@ def create_job_rect_at_board( ip_address: Optional[str] = None, machine_name: Optional[str] = None, keepalive: int = 45, max_dead_boards: int = 0) -> SpallocJob: + board: JsonObject if triad: x, y, z = triad board = {"x": int(x), "y": int(y), "z": int(z)} From 68fd7ae53ae7164505a377bab4b487d22777283e Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Tue, 19 Dec 2023 13:22:33 +0000 Subject: [PATCH 5/5] remove unused type --- spinnman/spalloc/spalloc_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index 2df61eb79..31731d8d1 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -28,7 +28,7 @@ from typing import ( Any, ContextManager, Callable, Dict, FrozenSet, Iterable, Iterator, List, Mapping, - Optional, Tuple, Union, cast) + Optional, Tuple, cast) from typing_extensions import TypeAlias from websocket import WebSocket # type: ignore from spinn_utilities.abstract_base import AbstractBase, abstractmethod