From d36db02bcefd3f132215ab05cbcaa36f3c5e3865 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Tue, 14 May 2024 02:10:36 +0300 Subject: [PATCH] Improve the VersionCheck static typing --- kernel_hardening_checker/__init__.py | 2 +- kernel_hardening_checker/engine.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/kernel_hardening_checker/__init__.py b/kernel_hardening_checker/__init__.py index a1c8e06..bb4af6c 100644 --- a/kernel_hardening_checker/__init__.py +++ b/kernel_hardening_checker/__init__.py @@ -55,7 +55,7 @@ def detect_kernel_version(fname: str) -> Tuple[TupleOrNone, str]: ver_str = parts[2].split('-', 1)[0] ver_numbers = ver_str.split('.') if len(ver_numbers) >= 3: - if all(map(lambda x: x.isdigit(), ver_numbers)): + if all(map(lambda x: x.isdecimal(), ver_numbers)): return tuple(map(int, ver_numbers)), 'OK' msg = f'failed to parse the version "{parts[2]}"' return None, msg diff --git a/kernel_hardening_checker/engine.py b/kernel_hardening_checker/engine.py index 0f18781..569809a 100644 --- a/kernel_hardening_checker/engine.py +++ b/kernel_hardening_checker/engine.py @@ -16,8 +16,8 @@ import sys from typing import Union, Optional, List, Dict, Tuple StrOrNone = Optional[str] -TupleOrNone = Optional[Tuple] -DictOrTuple = Union[Dict[str, str], Tuple] +TupleOrNone = Optional[Tuple[int, ...]] +DictOrTuple = Union[Dict[str, str], Tuple[int, ...]] StrOrBool = Union[str, bool] GREEN_COLOR = '\x1b[32m' @@ -161,16 +161,18 @@ class VersionCheck: assert(all(map(lambda x: isinstance(x, int), ver_expected))), \ f'invalid expected version "{ver_expected}" for VersionCheck (2)' self.ver_expected = ver_expected - self.ver = (0, 0, 0) # type: Tuple[int, int, int] + self.ver = (0, 0, 0) # type: Tuple[int, ...] self.result = None # type: str | None @property def opt_type(self) -> str: return 'version' - def set_state(self, data: Tuple) -> None: + def set_state(self, data: Tuple[int, ...]) -> None: assert(data and isinstance(data, tuple) and len(data) >= 3), \ - f'invalid version "{data}" for VersionCheck' + f'invalid version "{data}" for VersionCheck (1)' + assert(all(map(lambda x: isinstance(x, int), data))), \ + f'invalid version "{data}" for VersionCheck (2)' self.ver = data[:3] def check(self) -> None: -- 2.31.1