88import sys
99from collections import defaultdict
1010from pathlib import Path
11- from typing import NamedTuple
11+ from typing import NamedTuple , TypedDict
1212
1313
1414class IgnoreRule (NamedTuple ):
1515 file_path : str
16- count : int
16+ count : int # type: ignore[assignment]
1717 ignore_all : bool = False
1818 is_directory : bool = False
1919
2020
21+ class CompileWarning (TypedDict ):
22+ file : str
23+ line : str
24+ column : str
25+ message : str
26+ option : str
27+
28+
2129def parse_warning_ignore_file (file_path : str ) -> set [IgnoreRule ]:
2230 """
2331 Parses the warning ignore file and returns a set of IgnoreRules
2432 """
25- files_with_expected_warnings = set ()
33+ files_with_expected_warnings : set [ IgnoreRule ] = set ()
2634 with Path (file_path ).open (encoding = "UTF-8" ) as ignore_rules_file :
2735 files_with_expected_warnings = set ()
2836 for i , line in enumerate (ignore_rules_file ):
@@ -46,7 +54,7 @@ def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
4654 )
4755 sys .exit (1 )
4856 if ignore_all :
49- count = 0
57+ count = "0"
5058
5159 files_with_expected_warnings .add (
5260 IgnoreRule (
@@ -61,7 +69,7 @@ def extract_warnings_from_compiler_output(
6169 compiler_output : str ,
6270 compiler_output_type : str ,
6371 path_prefix : str = "" ,
64- ) -> list [dict ]:
72+ ) -> list [CompileWarning ]:
6573 """
6674 Extracts warnings from the compiler output based on compiler
6775 output type. Removes path prefix from file paths if provided.
@@ -78,8 +86,12 @@ def extract_warnings_from_compiler_output(
7886 r"(?P<file>.*):(?P<line>\d+):(?P<column>\d+): warning: "
7987 r"(?P<message>.*) (?P<option>\[-[^\]]+\])$"
8088 )
89+ else :
90+ raise RuntimeError (
91+ f"Unsupported compiler output type: { compiler_output_type } " ,
92+ )
8193 compiled_regex = re .compile (regex_pattern )
82- compiler_warnings = []
94+ compiler_warnings : list [ CompileWarning ] = []
8395 for i , line in enumerate (compiler_output .splitlines (), start = 1 ):
8496 if match := compiled_regex .match (line ):
8597 try :
@@ -100,7 +112,9 @@ def extract_warnings_from_compiler_output(
100112 return compiler_warnings
101113
102114
103- def get_warnings_by_file (warnings : list [dict ]) -> dict [str , list [dict ]]:
115+ def get_warnings_by_file (
116+ warnings : list [CompileWarning ],
117+ ) -> dict [str , list [CompileWarning ]]:
104118 """
105119 Returns a dictionary where the key is the file and the data is the
106120 warnings in that file. Does not include duplicate warnings for a
@@ -138,7 +152,7 @@ def is_file_ignored(
138152
139153def get_unexpected_warnings (
140154 ignore_rules : set [IgnoreRule ],
141- files_with_warnings : set [ IgnoreRule ],
155+ files_with_warnings : dict [ str , list [ CompileWarning ] ],
142156) -> int :
143157 """
144158 Returns failure status if warnings discovered in list of warnings
@@ -180,7 +194,7 @@ def get_unexpected_warnings(
180194
181195def get_unexpected_improvements (
182196 ignore_rules : set [IgnoreRule ],
183- files_with_warnings : set [ IgnoreRule ],
197+ files_with_warnings : dict [ str , list [ CompileWarning ] ],
184198) -> int :
185199 """
186200 Returns failure status if the number of warnings for a file is greater
0 commit comments