From 4d557d8b76d0fa5f9ea05daf2b5949f86c5101b7 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 3 Oct 2024 18:31:38 +0200 Subject: [PATCH] Typecheck main Always have tasks_list be a list, not potentially some fancier iterable. Bypass mypy's somewhat legitimate complaint about REFERENCE and DRIVER in task_class: they could potentially be instance attributes, but we rely on them being class attributes. Python does normally guarantee their existence as class attributes (unless a derived class explicitly deletes them), but they could be overridden by an instance attribute; that's just something we don't do, so the class attribute's value is legitimate. We can't expect mypy to know that, so work around its complaint. Signed-off-by: Gilles Peskine --- tests/scripts/analyze_outcomes.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/scripts/analyze_outcomes.py b/tests/scripts/analyze_outcomes.py index 9f8c2c3c71..f681a30ba2 100755 --- a/tests/scripts/analyze_outcomes.py +++ b/tests/scripts/analyze_outcomes.py @@ -761,8 +761,7 @@ KNOWN_TASKS = { 'analyze_block_cipher_dispatch': DriverVSReference_block_cipher_dispatch, } - -def main(): +def main() -> None: main_results = Results() try: @@ -789,7 +788,7 @@ def main(): sys.exit(0) if options.specified_tasks == 'all': - tasks_list = KNOWN_TASKS.keys() + tasks_list = list(KNOWN_TASKS.keys()) else: tasks_list = re.split(r'[, ]+', options.specified_tasks) for task_name in tasks_list: @@ -810,9 +809,16 @@ def main(): if not issubclass(task_class, DriverVSReference): sys.stderr.write("please provide valid outcomes file for {}.\n".format(task_name)) sys.exit(2) + # mypy isn't smart enough to know that REFERENCE and DRIVER + # are *class* attributes of all classes derived from + # DriverVSReference. (It would be smart enough if we had an + # instance of task_class, but we can't construct an instance + # until we have the outcome data, so at this point we only + # have the class.) So we use indirection to access the class + # attributes. execute_reference_driver_tests(main_results, - task_class.REFERENCE, - task_class.DRIVER, + getattr(task_class, 'REFERENCE'), + getattr(task_class, 'DRIVER'), options.outcomes) outcomes = read_outcome_file(options.outcomes)