Skip to content

Commit

Permalink
more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Jun 27, 2024
1 parent 39bf8bf commit 37f5ca4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions spalloc_client/protocol_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return False

def _get_connection(self, timeout):
def _get_connection(self, timeout: Optional[int]) -> socket:
if self._dead:
raise OSError(errno.ENOTCONN, "not connected")
connect_needed = False
Expand All @@ -141,7 +141,7 @@ def _get_connection(self, timeout):
sock.settimeout(timeout)
return sock

def _do_connect(self, sock):
def _do_connect(self, sock: socket):
success = False
try:
sock.connect((self._hostname, self._port))
Expand All @@ -151,10 +151,10 @@ def _do_connect(self, sock):
raise
return success

def _has_open_socket(self):
def _has_open_socket(self) -> bool:
return self._local.sock is not None

def connect(self, timeout=None):
def connect(self, timeout: Optional[int] = None):
"""(Re)connect to the server.
Raises
Expand All @@ -168,7 +168,7 @@ def connect(self, timeout=None):
self._dead = False
self._connect(timeout)

def _connect(self, timeout):
def _connect(self, timeout: Optional[int]) -> socket:
""" Try to (re)connect to the server.
"""
try:
Expand Down
6 changes: 3 additions & 3 deletions spalloc_client/scripts/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
from collections import defaultdict
import argparse
import sys
from typing import List
from typing import Any, Dict, List

from spalloc_client import __version__, ProtocolClient
from spalloc_client.term import (
Terminal, render_table, render_definitions, render_boards, render_cells,
DEFAULT_BOARD_EDGES, TableList)
from .support import Terminate, Script
from spalloc_client.scripts.support import Terminate, Script


def generate_keys(alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
Expand Down Expand Up @@ -233,7 +233,7 @@ def get_and_display_machine_info(self, client: ProtocolClient,
else:
show_machine(t, machines, jobs, args.machine, not args.detailed)

def get_parser(self, cfg):
def get_parser(self, cfg: Dict[str, Any]):
parser = argparse.ArgumentParser(
description="Get the state of individual machines.")
parser.add_argument(
Expand Down
5 changes: 3 additions & 2 deletions spalloc_client/scripts/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
import argparse
import sys

from spinn_utilities.typing.json import JsonObject
from spinn_utilities.typing.json import JsonObjectArray

from spalloc_client import __version__, JobState, ProtocolClient
from spalloc_client.term import Terminal, render_table, TableList
from spalloc_client._utils import render_timestamp
from .support import Script


def render_job_list(t: Terminal, jobs: JsonObject, args: argparse.Namespace):
def render_job_list(t: Terminal, jobs: JsonObjectArray,
args: argparse.Namespace):
""" Return a human-readable process listing.
Parameters
Expand Down
9 changes: 6 additions & 3 deletions spalloc_client/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

# pylint: disable=wrong-spelling-in-docstring

TableList: TypeAlias = List[Union[str,
Tuple[Callable[[Union[int, str]], str]]]]

TableFunction: TypeAlias = Callable[[Union[int, str]], str]
TableValue: TypeAlias = Union[int, str]
TableItem: TypeAlias = Union[TableValue, Tuple[TableFunction, TableValue]]
TableList: TypeAlias = List[TableItem]

class ANSIDisplayAttributes(IntEnum):
""" Code numbers of ANSI display attributes for use with `ESC[...m`\
Expand Down Expand Up @@ -215,6 +216,7 @@ def render_table(table: TableList, column_sep: str = " "):
# Determine maximum column widths
column_widths = defaultdict(lambda: 0)
for row in table:
column: TableItem
for i, column in enumerate(row):
if isinstance(column, str):
string = column
Expand All @@ -229,6 +231,7 @@ def render_table(table: TableList, column_sep: str = " "):
for row in table:
rendered_row = []
out.append(rendered_row)
f: TableFunction
for i, column in enumerate(row):
# Get string length and formatted string
if isinstance(column, str):
Expand Down

0 comments on commit 37f5ca4

Please sign in to comment.