From 70f2a672ee21b1a293732eca9c7c34783908d011 Mon Sep 17 00:00:00 2001 From: disinvite Date: Mon, 11 Dec 2023 22:41:03 -0500 Subject: [PATCH] Type hints, working test for byname ignore --- tools/isledecomp/isledecomp/parser/error.py | 4 ++-- tools/isledecomp/isledecomp/parser/linter.py | 13 +++++++------ tools/isledecomp/isledecomp/parser/parser.py | 4 ++-- tools/isledecomp/tests/test_linter.py | 9 ++++++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tools/isledecomp/isledecomp/parser/error.py b/tools/isledecomp/isledecomp/parser/error.py index 34962eb0..638a806e 100644 --- a/tools/isledecomp/isledecomp/parser/error.py +++ b/tools/isledecomp/isledecomp/parser/error.py @@ -1,5 +1,5 @@ -from __future__ import annotations # python <3.10 compatibility from enum import Enum +from typing import Optional from dataclasses import dataclass @@ -62,7 +62,7 @@ class ParserError(Enum): class ParserAlert: code: ParserError line_number: int - line: str | None = None + line: Optional[str] = None def is_warning(self) -> bool: return self.code.value < ParserError.DECOMP_ERROR_START.value diff --git a/tools/isledecomp/isledecomp/parser/linter.py b/tools/isledecomp/isledecomp/parser/linter.py index ea24c69c..99c8e272 100644 --- a/tools/isledecomp/isledecomp/parser/linter.py +++ b/tools/isledecomp/isledecomp/parser/linter.py @@ -1,3 +1,4 @@ +from typing import List, Optional from .parser import DecompParser from .error import ParserAlert, ParserError @@ -8,11 +9,11 @@ def get_checkorder_filter(module): class DecompLinter: - def __init__(self): - self.alerts = [] + def __init__(self) -> None: + self.alerts: List[ParserAlert] = [] self._parser = DecompParser() - self._filename = "" - self._module = None + self._filename: str = "" + self._module: Optional[str] = None def reset(self): self.alerts = [] @@ -53,7 +54,7 @@ def _check_function_order(self): last_offset = fun.offset - def _check_symbol_uniqueness(self): + def _check_offset_uniqueness(self): # TODO pass @@ -85,7 +86,7 @@ def check_lines(self, lines, filename, module=None): if self._module is not None: self._check_byname_allowed() - self._check_symbol_uniqueness() + self._check_offset_uniqueness() if not self.file_is_header(): self._check_function_order() diff --git a/tools/isledecomp/isledecomp/parser/parser.py b/tools/isledecomp/isledecomp/parser/parser.py index d133014c..336d4b0c 100644 --- a/tools/isledecomp/isledecomp/parser/parser.py +++ b/tools/isledecomp/isledecomp/parser/parser.py @@ -56,7 +56,7 @@ def marker_is_vtable(marker: DecompMarker) -> bool: class MarkerDict: - def __init__(self): + def __init__(self) -> None: self.markers: dict = {} def insert(self, marker: DecompMarker) -> bool: @@ -80,7 +80,7 @@ class DecompParser: # pylint: disable=too-many-instance-attributes # Could combine output lists into a single list to get under the limit, # but not right now - def __init__(self): + def __init__(self) -> None: # The lists to be populated as we parse self.functions: List[ParserFunction] = [] self.vtables: List[ParserVtable] = [] diff --git a/tools/isledecomp/tests/test_linter.py b/tools/isledecomp/tests/test_linter.py index 6fca10fb..cb3b8f27 100644 --- a/tools/isledecomp/tests/test_linter.py +++ b/tools/isledecomp/tests/test_linter.py @@ -47,9 +47,12 @@ def test_byname_ignored(linter): "// FUNCTION: TEST 0x2000", "void function2() {}", ] - # TODO: This will fail after enforcing that bynames belong in headers - assert linter.check_lines(lines, "test.cpp", "TEST") is True - assert len(linter.alerts) == 0 + # This will fail because byname lookup does not belong in the cpp file + assert linter.check_lines(lines, "test.cpp", "TEST") is False + # but it should not fail for function order. + assert all( + alert.code != ParserError.FUNCTION_OUT_OF_ORDER for alert in linter.alerts + ) def test_module_isolation(linter):