Skip to content

Commit 8516b28

Browse files
committed
WIP common: introduce type aliases for HostAddress and DiskDevName
Will help readability and improve typechecking. FIXME: start to use them
1 parent c000497 commit 8516b28

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

lib/commands.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import List, Literal, overload, Union
66

77

8+
from lib.common import HostAddress
89
import lib.config as config
910
from lib.netutil import wrap_ip
1011

@@ -139,27 +140,32 @@ def _ssh(hostname_or_ip, cmd, check, simple_output, suppress_fingerprint_warning
139140
# This function is kept short for shorter pytest traces upon SSH failures, which are common,
140141
# as pytest prints the whole function definition that raised the SSHCommandFailed exception
141142
@overload
142-
def ssh(hostname_or_ip: str, cmd: Union[str, List[str]], *, check: bool = True, simple_output: Literal[True] = True,
143+
def ssh(hostname_or_ip: HostAddress, cmd: Union[str, List[str]], *, check: bool = True,
144+
simple_output: Literal[True] = True,
143145
suppress_fingerprint_warnings: bool = True, background: Literal[False] = False,
144146
decode: Literal[True] = True, options: List[str] = []) -> str:
145147
...
146148
@overload
147-
def ssh(hostname_or_ip: str, cmd: Union[str, List[str]], *, check: bool = True, simple_output: Literal[True] = True,
149+
def ssh(hostname_or_ip: HostAddress, cmd: Union[str, List[str]], *, check: bool = True,
150+
simple_output: Literal[True] = True,
148151
suppress_fingerprint_warnings: bool = True, background: Literal[False] = False,
149152
decode: Literal[False], options: List[str] = []) -> bytes:
150153
...
151154
@overload
152-
def ssh(hostname_or_ip: str, cmd: Union[str, List[str]], *, check: bool = True, simple_output: Literal[False],
155+
def ssh(hostname_or_ip: HostAddress, cmd: Union[str, List[str]], *, check: bool = True,
156+
simple_output: Literal[False],
153157
suppress_fingerprint_warnings: bool = True, background: Literal[False] = False,
154158
decode: bool = True, options: List[str] = []) -> SSHResult:
155159
...
156160
@overload
157-
def ssh(hostname_or_ip: str, cmd: Union[str, List[str]], *, check: bool = True, simple_output: Literal[False],
161+
def ssh(hostname_or_ip: HostAddress, cmd: Union[str, List[str]], *, check: bool = True,
162+
simple_output: Literal[False],
158163
suppress_fingerprint_warnings: bool = True, background: Literal[True],
159164
decode: bool = True, options: List[str] = []) -> None:
160165
...
161166
@overload
162-
def ssh(hostname_or_ip: str, cmd: Union[str, List[str]], *, check=True, simple_output: bool = True,
167+
def ssh(hostname_or_ip: HostAddress, cmd: Union[str, List[str]], *, check=True,
168+
simple_output: bool = True,
163169
suppress_fingerprint_warnings=True, background: bool = False,
164170
decode: bool = True, options: List[str] = []) \
165171
-> Union[str, bytes, SSHResult, None]:

lib/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import getpass
24
import inspect
35
import itertools
@@ -16,9 +18,13 @@
1618
import lib.commands as commands
1719
if TYPE_CHECKING:
1820
import lib.host
21+
from lib.typing import TypeAlias
1922

2023
T = TypeVar("T")
2124

25+
HostAddress: TypeAlias = str
26+
DiskDevName: TypeAlias = str
27+
2228
class PackageManagerEnum(Enum):
2329
UNKNOWN = 1
2430
RPM = 2

lib/host.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
if TYPE_CHECKING:
1616
import lib.pool
1717

18+
from lib.common import DiskDevName
1819
from lib.common import _param_add, _param_clear, _param_get, _param_remove, _param_set, strtobool
1920
from lib.common import safe_split, strip_suffix, to_xapi_bool, wait_for, wait_for_not
2021
from lib.common import prefix_object_name
@@ -534,13 +535,13 @@ def management_pif(self):
534535
uuid = self.xe('pif-list', {'management': True, 'host-uuid': self.uuid}, minimal=True)
535536
return pif.PIF(uuid, self)
536537

537-
def disks(self):
538+
def disks(self) -> list[DiskDevName]:
538539
""" List of SCSI disks, e.g ['sda', 'sdb', 'nvme0n1']. """
539540
disks = self.ssh(['lsblk', '-nd', '-I', '8,259', '--output', 'NAME']).splitlines()
540541
disks.sort()
541542
return disks
542543

543-
def disk_is_available(self, disk: str) -> bool:
544+
def disk_is_available(self, disk: DiskDevName) -> bool:
544545
"""
545546
Check if a disk is unmounted and appears available for use.
546547
@@ -552,7 +553,7 @@ def disk_is_available(self, disk: str) -> bool:
552553
"""
553554
return len(self.ssh(['lsblk', '-n', '-o', 'MOUNTPOINT', '/dev/' + disk]).strip()) == 0
554555

555-
def available_disks(self, blocksize=512):
556+
def available_disks(self, blocksize: int = 512) -> list[DiskDevName]:
556557
"""
557558
Return a list of available disks for formatting, creating SRs or such.
558559

lib/pool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from packaging import version
77

88
import lib.commands as commands
9+
from lib.common import HostAddress
910
from lib.common import _param_get, _param_set, safe_split, wait_for, wait_for_not
1011
from lib.host import Host
1112
from lib.sr import SR
@@ -14,7 +15,7 @@
1415
class Pool:
1516
xe_prefix = "pool"
1617

17-
def __init__(self, master_hostname_or_ip: str) -> None:
18+
def __init__(self, master_hostname_or_ip: HostAddress) -> None:
1819
master = Host(self, master_hostname_or_ip)
1920
assert master.is_master(), f"Host {master_hostname_or_ip} is not a master host. Aborting."
2021
self.master = master

lib/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import sys
22
from typing import TypedDict
33

4+
if sys.version_info >= (3, 10):
5+
from typing import TypeAlias
6+
else:
7+
from typing_extensions import TypeAlias
8+
49
if sys.version_info >= (3, 11):
510
from typing import NotRequired
611
else:

0 commit comments

Comments
 (0)