From 90c867844a03ffd08647ee358ddc94157ef4c469 Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 29 Sep 2025 20:18:53 -0400 Subject: [PATCH 1/3] Add type annotations for _formatting module Signed-off-by: mulhern --- src/stratis_cli/_actions/_formatting.py | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/stratis_cli/_actions/_formatting.py b/src/stratis_cli/_actions/_formatting.py index 272685390..abc76f327 100644 --- a/src/stratis_cli/_actions/_formatting.py +++ b/src/stratis_cli/_actions/_formatting.py @@ -17,9 +17,12 @@ # isort: STDLIB import sys +from typing import Any, Callable, List, Optional from uuid import UUID # isort: THIRDPARTY +from dbus import Struct +from justbytes import Range from wcwidth import wcswidth # placeholder for tables where a desired value was not obtained from stratisd @@ -33,7 +36,7 @@ TOTAL_USED_FREE = "Total / Used / Free" -def size_triple(size, used): +def size_triple(size: Optional[Range], used: Optional[Range]) -> str: """ Given size and used, return a properly formatted string Total/ Used / Free @@ -53,7 +56,7 @@ def size_triple(size, used): ) -def get_property(prop, to_repr, default): +def get_property(prop: Struct, to_repr: Callable, default: Optional[Any]): """ Get a representation of an optional D-Bus property. An optional D-Bus property is one that may be unknown to stratisd. @@ -70,7 +73,7 @@ def get_property(prop, to_repr, default): return to_repr(value) if valid else default -def _get_column_len(column_width, entry_len, entry_width): +def _get_column_len(column_width: int, entry_len: int, entry_width: int) -> int: """ From the desired column width in cells and the item to be printed, calculate the required number of characters to pass to the format method. @@ -94,7 +97,13 @@ def _get_column_len(column_width, entry_len, entry_width): return column_width - (entry_width - entry_len) -def _print_row(file, row, row_widths, column_widths, column_alignments): +def _print_row( + file: Any, + row: Any, + row_widths: List[int], + column_widths: List[int], + column_alignments: List[str], +): """ Print a single row in a table. The row might be the header row, or a row of data items. @@ -117,7 +126,12 @@ def _print_row(file, row, row_widths, column_widths, column_alignments): print(" ".join(entries), end="", file=file) -def print_table(column_headings, row_entries, alignment, file=sys.stdout): +def print_table( + column_headings: List[str], + row_entries: List[Any], + alignment: List[str], + file=sys.stdout, +): """ Given the column headings and the row_entries, print a table. Align according to alignment specification and always pad with 2 spaces. @@ -156,7 +170,7 @@ def print_table(column_headings, row_entries, alignment, file=sys.stdout): print(file=file) -def get_uuid_formatter(unhyphenated): +def get_uuid_formatter(unhyphenated: bool) -> Callable: """ Get a function to format UUIDs. From 5ce038dfcb62b16a367c8b86f7363366384a3860 Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 29 Sep 2025 20:32:07 -0400 Subject: [PATCH 2/3] Make type of size param reflect true usage Adapt body of method accordingly Signed-off-by: mulhern --- src/stratis_cli/_actions/_formatting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stratis_cli/_actions/_formatting.py b/src/stratis_cli/_actions/_formatting.py index abc76f327..af11b62af 100644 --- a/src/stratis_cli/_actions/_formatting.py +++ b/src/stratis_cli/_actions/_formatting.py @@ -36,7 +36,7 @@ TOTAL_USED_FREE = "Total / Used / Free" -def size_triple(size: Optional[Range], used: Optional[Range]) -> str: +def size_triple(size: Range, used: Optional[Range]) -> str: """ Given size and used, return a properly formatted string Total/ Used / Free @@ -47,10 +47,10 @@ def size_triple(size: Optional[Range], used: Optional[Range]) -> str: :rtype: str :returns: formatted string for display """ - free = None if size is None or used is None else size - used + free = None if used is None else size - used return ( - f"{TABLE_FAILURE_STRING if size is None else size} / " + f"{size} / " f"{TABLE_FAILURE_STRING if used is None else used} / " f"{TABLE_FAILURE_STRING if free is None else free}" ) From 77f5e2cd3bb91a55f86c15d4e950c5fe051ec7bb Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 29 Sep 2025 20:42:35 -0400 Subject: [PATCH 3/3] Refactor filesystem_size_quartet Also add type annotations for the method Signed-off-by: mulhern --- src/stratis_cli/_actions/_list_filesystem.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/stratis_cli/_actions/_list_filesystem.py b/src/stratis_cli/_actions/_list_filesystem.py index cd2252c37..1a82e3e71 100644 --- a/src/stratis_cli/_actions/_list_filesystem.py +++ b/src/stratis_cli/_actions/_list_filesystem.py @@ -17,7 +17,7 @@ # isort: STDLIB from abc import ABC, abstractmethod -from typing import Any, Callable, Dict, List +from typing import Any, Callable, Dict, List, Optional # isort: THIRDPARTY from dateutil import parser as date_parser @@ -124,26 +124,26 @@ def display(self): List the filesystems. """ - def filesystem_size_quartet(dbus_props): + def filesystem_size_quartet( + total: Range, used: Optional[Range], limit: Optional[Range] + ) -> str: """ Calculate the triple to display for filesystem size. - :param dbus_props: filesystem D-Bus properties - :type dbus_props: MOFilesystem - :returns: a string a formatted string showing all three values :rtype: str """ - total = Range(dbus_props.Size()) - used = get_property(dbus_props.Used(), Range, None) - limit = get_property(dbus_props.SizeLimit(), Range, None) return f'{size_triple(total, used)} / {"None" if limit is None else limit}' tables = [ ( self.pool_object_path_to_pool_name[mofilesystem.Pool()], mofilesystem.Name(), - filesystem_size_quartet(mofilesystem), + filesystem_size_quartet( + Range(mofilesystem.Size()), + get_property(mofilesystem.Used(), Range, None), + get_property(mofilesystem.SizeLimit(), Range, None), + ), mofilesystem.Devnode(), self.uuid_formatter(mofilesystem.Uuid()), )