Skip to content

Commit bd53b75

Browse files
authored
Merge pull request #1267 from mulkieran/add-type-annotations-pool-list
Add type annotations for existing methods in _list_pool.py
2 parents 335f25a + 6571b63 commit bd53b75

1 file changed

Lines changed: 66 additions & 33 deletions

File tree

src/stratis_cli/_actions/_list_pool.py

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
import json
2020
import os
2121
from abc import ABC, abstractmethod
22-
from typing import List, Optional, Union
22+
from typing import (
23+
Any,
24+
Callable,
25+
Iterable,
26+
List,
27+
Mapping,
28+
)
29+
from uuid import UUID
2330

2431
# isort: THIRDPARTY
2532
from justbytes import Range
@@ -30,8 +37,9 @@
3037
PoolDeviceSizeChangeAlert,
3138
PoolEncryptionAlert,
3239
)
40+
from .._constants import PoolId
3341
from .._errors import StratisCliResourceNotFoundError
34-
from .._stratisd_constants import MetadataVersion, PoolActionAvailability
42+
from .._stratisd_constants import ClevisInfo, MetadataVersion, PoolActionAvailability
3543
from ._connection import get_object
3644
from ._constants import TOP_OBJECT
3745
from ._formatting import (
@@ -41,6 +49,7 @@
4149
print_table,
4250
)
4351
from ._utils import (
52+
EncryptionInfo,
4453
EncryptionInfoClevis,
4554
EncryptionInfoKeyDescription,
4655
PoolFeature,
@@ -50,14 +59,14 @@
5059
)
5160

5261

53-
def _metadata_version(mopool) -> Optional[MetadataVersion]:
62+
def _metadata_version(mopool: Any) -> MetadataVersion | None:
5463
try:
5564
return MetadataVersion(int(mopool.MetadataVersion()))
5665
except ValueError: # pragma: no cover
5766
return None
5867

5968

60-
def _volume_key_loaded(mopool) -> Union[tuple[bool, bool], tuple[bool, str]]:
69+
def _volume_key_loaded(mopool: Any) -> tuple[bool, bool] | tuple[bool, str]:
6170
"""
6271
The string result is an error message indicating that the volume key
6372
state is unknown.
@@ -70,12 +79,12 @@ def _volume_key_loaded(mopool) -> Union[tuple[bool, bool], tuple[bool, str]]:
7079

7180
# This method is only used with legacy pools
7281
def _non_existent_or_inconsistent_to_str(
73-
value,
82+
value: EncryptionInfo | None,
7483
*,
75-
inconsistent_str="inconsistent",
76-
non_existent_str="N/A",
77-
interp=lambda x: str(x), # pylint: disable=unnecessary-lambda
78-
): # pragma: no cover
84+
inconsistent_str: str = "inconsistent",
85+
non_existent_str: str = "N/A",
86+
interp: Callable[[Any], str] = str,
87+
) -> str: # pragma: no cover
7988
"""
8089
Process dbus result that encodes both inconsistency and existence of the
8190
value.
@@ -87,15 +96,18 @@ def _non_existent_or_inconsistent_to_str(
8796
:returns: a string to print
8897
:rtype: str
8998
"""
99+
if value is None:
100+
return non_existent_str
101+
90102
if not value.consistent():
91103
return inconsistent_str
92104

93-
value = value.value
105+
inner_value = value.value
94106

95-
if value is None:
107+
if inner_value is None:
96108
return non_existent_str
97109

98-
return interp(value)
110+
return interp(inner_value)
99111

100112

101113
class TokenSlotInfo: # pylint: disable=too-few-public-methods
@@ -105,7 +117,13 @@ class TokenSlotInfo: # pylint: disable=too-few-public-methods
105117
token and then printed.
106118
"""
107119

108-
def __init__(self, token_slot, *, key=None, clevis=None):
120+
def __init__(
121+
self,
122+
token_slot: int,
123+
*,
124+
key: str | None = None,
125+
clevis: tuple[str, Any] | None = None,
126+
):
109127
"""
110128
Initialize either information about a key or about a Clevis
111129
configuration for purposes of printing later.
@@ -121,7 +139,7 @@ def __init__(self, token_slot, *, key=None, clevis=None):
121139
self.key = key
122140
self.clevis = clevis
123141

124-
def __str__(self):
142+
def __str__(self) -> str:
125143
return f"Token Slot: {self.token_slot}{os.linesep}" + (
126144
f" Key Description: {self.key}"
127145
if self.clevis is None
@@ -137,15 +155,15 @@ class DefaultAlerts: # pylint: disable=too-few-public-methods
137155
Alerts to display for a started pool.
138156
"""
139157

140-
def __init__(self, devs):
158+
def __init__(self, devs: Iterable[tuple[Any, Mapping[str, Mapping[str, Any]]]]):
141159
"""
142160
The initializer.
143161
144162
:param devs: result of GetManagedObjects
145163
"""
146164
(self.increased, self.decreased) = DefaultAlerts._pools_with_changed_devs(devs)
147165

148-
def alert_codes(self, pool_object_path, mopool) -> List[PoolAlertType]:
166+
def alert_codes(self, pool_object_path: str, mopool: Any) -> List[PoolAlertType]:
149167
"""
150168
Return alert code objects for a pool.
151169
@@ -191,7 +209,9 @@ def alert_codes(self, pool_object_path, mopool) -> List[PoolAlertType]:
191209
)
192210

193211
@staticmethod
194-
def _pools_with_changed_devs(devs_to_search):
212+
def _pools_with_changed_devs(
213+
devs_to_search: Iterable[tuple[Any, Mapping[str, Mapping[str, Any]]]]
214+
) -> tuple[set[str], set[str]]:
195215
"""
196216
Returns a tuple of sets containing (1) pools that have a device that
197217
has increased in size and (2) pools that have a device that has
@@ -221,7 +241,7 @@ def _pools_with_changed_devs(devs_to_search):
221241

222242
@staticmethod
223243
def _from_sets(
224-
pool_object_path, increased, decreased
244+
pool_object_path: str, increased: set[str], decreased: set[str]
225245
) -> List[PoolDeviceSizeChangeAlert]:
226246
"""
227247
Get the code from sets and one pool object path.
@@ -248,7 +268,12 @@ def _from_sets(
248268
return []
249269

250270

251-
def list_pools(uuid_formatter, *, stopped=False, selection=None):
271+
def list_pools(
272+
uuid_formatter: Callable[[str | UUID], str],
273+
*,
274+
stopped: bool = False,
275+
selection: PoolId | None = None,
276+
):
252277
"""
253278
List the specified information about pools.
254279
:param uuid_formatter: how to format UUIDs
@@ -270,7 +295,7 @@ def list_pools(uuid_formatter, *, stopped=False, selection=None):
270295
klass.display()
271296

272297

273-
def _clevis_to_str(clevis_info): # pragma: no cover
298+
def _clevis_to_str(clevis_info: ClevisInfo) -> str: # pragma: no cover
274299
"""
275300
:param ClevisInfo clevis_info: the Clevis info to stringify
276301
:return: a string that represents the clevis info
@@ -306,7 +331,7 @@ class DefaultDetail(Default): # pylint: disable=too-few-public-methods
306331
List one pool with a detail view.
307332
"""
308333

309-
def __init__(self, uuid_formatter, selection):
334+
def __init__(self, uuid_formatter: Callable[[str | UUID], str], selection: PoolId):
310335
"""
311336
Initializer.
312337
:param uuid_formatter: function to format a UUID str or UUID
@@ -317,7 +342,7 @@ def __init__(self, uuid_formatter, selection):
317342
self.selection = selection
318343

319344
def _print_detail_view(
320-
self, pool_object_path, mopool, alerts: DefaultAlerts
345+
self, pool_object_path: str, mopool: Any, alerts: DefaultAlerts
321346
): # pylint: disable=too-many-locals
322347
"""
323348
Print the detailed view for a single pool.
@@ -441,7 +466,7 @@ class DefaultTable(Default): # pylint: disable=too-few-public-methods
441466
List several pools with a table view.
442467
"""
443468

444-
def __init__(self, uuid_formatter):
469+
def __init__(self, uuid_formatter: Callable[[str | UUID], str]):
445470
"""
446471
Initializer.
447472
:param uuid_formatter: function to format a UUID str or UUID
@@ -458,7 +483,7 @@ def display(self):
458483

459484
proxy = get_object(TOP_OBJECT)
460485

461-
def physical_size_triple(mopool):
486+
def physical_size_triple(mopool: Any) -> str:
462487
"""
463488
Calculate the triple to display for total physical size.
464489
@@ -487,7 +512,7 @@ def physical_size_triple(mopool):
487512
)
488513
)
489514

490-
def properties_string(mopool):
515+
def properties_string(mopool: Any) -> str:
491516
"""
492517
Make a string encoding some important properties of the pool
493518
@@ -497,7 +522,7 @@ def properties_string(mopool):
497522
:type props_map: dict of str * any
498523
"""
499524

500-
def gen_string(has_property, code):
525+
def gen_string(has_property: bool, code: str) -> str:
501526
"""
502527
Generate the display string for a boolean property
503528
@@ -561,7 +586,7 @@ class Stopped(ListPool): # pylint: disable=too-few-public-methods
561586
"""
562587

563588
@staticmethod
564-
def _pool_name(maybe_value):
589+
def _pool_name(maybe_value: str | None) -> str:
565590
"""
566591
Return formatted string for pool name.
567592
@@ -571,7 +596,7 @@ def _pool_name(maybe_value):
571596
return "<UNAVAILABLE>" if maybe_value is None else maybe_value
572597

573598
@staticmethod
574-
def _metadata_version_str(maybe_value):
599+
def _metadata_version_str(maybe_value: MetadataVersion | None) -> str:
575600
"""
576601
Return formatted string for metadata version.
577602
@@ -586,7 +611,7 @@ class StoppedDetail(Stopped): # pylint: disable=too-few-public-methods
586611
Detailed view of one stopped pool.
587612
"""
588613

589-
def __init__(self, uuid_formatter, selection):
614+
def __init__(self, uuid_formatter: Callable[[str | UUID], str], selection: PoolId):
590615
"""
591616
Initializer.
592617
:param uuid_formatter: function to format a UUID str or UUID
@@ -596,7 +621,7 @@ def __init__(self, uuid_formatter, selection):
596621
self.uuid_formatter = uuid_formatter
597622
self.selection = selection
598623

599-
def _print_detail_view(self, pool_uuid, pool):
624+
def _print_detail_view(self, pool_uuid: str, pool: StoppedPool):
600625
"""
601626
Print detailed view of a stopped pool.
602627
@@ -687,7 +712,7 @@ class StoppedTable(Stopped): # pylint: disable=too-few-public-methods
687712
Table view of one or many stopped pools.
688713
"""
689714

690-
def __init__(self, uuid_formatter):
715+
def __init__(self, uuid_formatter: Callable[[str | UUID], str]):
691716
"""
692717
Initializer.
693718
:param uuid_formatter: function to format a UUID str or UUID
@@ -703,7 +728,11 @@ def display(self):
703728

704729
stopped_pools = fetch_stopped_pools_property(proxy)
705730

706-
def clevis_str(value, metadata_version, features):
731+
def clevis_str(
732+
value: Any | None,
733+
metadata_version: MetadataVersion | None,
734+
features: frozenset[PoolFeature] | None,
735+
) -> str:
707736
if metadata_version is MetadataVersion.V2:
708737
return (
709738
"<UNKNOWN>"
@@ -727,7 +756,11 @@ def clevis_str(value, metadata_version, features):
727756
interp=lambda _: "present",
728757
) # pragma: no cover
729758

730-
def key_description_str(value, metadata_version, features):
759+
def key_description_str(
760+
value: EncryptionInfoKeyDescription | None,
761+
metadata_version: MetadataVersion | None,
762+
features: frozenset[PoolFeature] | None,
763+
) -> str:
731764
if metadata_version is MetadataVersion.V2:
732765
return (
733766
"<UNKNOWN>"

0 commit comments

Comments
 (0)