Fix mypy typing warnings for ChecklistObjType
authorAlexander Popov <alex.popov@linux.com>
Mon, 13 May 2024 15:16:37 +0000 (18:16 +0300)
committerAlexander Popov <alex.popov@linux.com>
Mon, 13 May 2024 16:44:21 +0000 (19:44 +0300)
kernel_hardening_checker/__init__.py
kernel_hardening_checker/engine.py

index ca163b7db5a077d0895faf4baa6f161f5b16e9bd..601619ea9ac321c568fe85c8311c1a5257e58bf6 100644 (file)
@@ -100,14 +100,21 @@ def print_checklist(mode: StrOrNone, checklist: List[ChecklistObjType], with_res
     print('=' * sep_line_len)
 
     # table contents
+    ok_count = 0
+    fail_count = 0
     for opt in checklist:
         if with_results:
-            if mode == 'show_ok':
-                if not opt.result.startswith('OK'):
+            assert(opt.result), f'unexpected empty result of {opt.name} check'
+            if opt.result.startswith('OK'):
+                ok_count += 1
+                if mode == 'show_fail':
                     continue
-            if mode == 'show_fail':
-                if not opt.result.startswith('FAIL'):
+            elif opt.result.startswith('FAIL'):
+                fail_count += 1
+                if mode == 'show_ok':
                     continue
+            else:
+                assert(False), f'unexpected result "{opt.result}" of {opt.name} check'
         opt.table_print(mode, with_results)
         print()
         if mode == 'verbose':
@@ -116,9 +123,7 @@ def print_checklist(mode: StrOrNone, checklist: List[ChecklistObjType], with_res
 
     # final score
     if with_results:
-        fail_count = len(list(filter(lambda opt: opt.result.startswith('FAIL'), checklist)))
         fail_suppressed = ''
-        ok_count = len(list(filter(lambda opt: opt.result.startswith('OK'), checklist)))
         ok_suppressed = ''
         if mode == 'show_ok':
             fail_suppressed = ' (suppressed in output)'
index b6beb6c17fc591b05d53a07cdda49681d5619a81..a816c2911a16e74fbff6aa5612267cd8864980ab 100644 (file)
@@ -388,8 +388,8 @@ def populate_with_data(checklist: List[ChecklistObjType], data: TupleOrOrderedDi
 def override_expected_value(checklist: List[ChecklistObjType], name: str, new_val: str) -> None:
     for opt in checklist:
         if opt.name == name:
-            assert(opt.opt_type in ('kconfig', 'cmdline', 'sysctl')), \
-                   f'overriding an expected value for "{opt.opt_type}" checks is not supported yet'
+            assert(isinstance(opt, SimpleNamedOptCheckTypes)), \
+                   f'overriding an expected value for {opt}" is not supported yet'
             opt.expected = new_val
 
 
@@ -402,17 +402,21 @@ def print_unknown_options(checklist: List[ChecklistObjType], parsed_options: Ord
     known_options = []
 
     for o1 in checklist:
-        if o1.opt_type != 'complex':
+        if isinstance(o1, SimpleOptCheckTypes):
+            assert(o1.opt_type != 'complex'), f'{o1} with complex opt_type'
+            assert(not isinstance(o1, VersionCheck)), 'single VersionCheck in checklist'
             known_options.append(o1.name)
             continue
         for o2 in o1.opts:
-            if o2.opt_type != 'complex':
+            if isinstance(o2, SimpleOptCheckTypes):
+                assert(o2.opt_type != 'complex'), f'{o2} with complex opt_type'
                 if hasattr(o2, 'name'):
                     known_options.append(o2.name)
                 continue
             for o3 in o2.opts:
-                assert(o3.opt_type != 'complex'), \
+                assert(isinstance(o3, SimpleOptCheckTypes)), \
                        f'unexpected ComplexOptCheck inside {o2.name}'
+                assert(o3.opt_type != 'complex'), f'{o3} with complex opt_type'
                 if hasattr(o3, 'name'):
                     known_options.append(o3.name)