8
8
import sys
9
9
from collections import defaultdict
10
10
from pathlib import Path
11
- from typing import NamedTuple
11
+ from typing import NamedTuple , TypedDict
12
12
13
13
14
14
class IgnoreRule (NamedTuple ):
15
15
file_path : str
16
- count : int
16
+ count : int # type: ignore[assignment]
17
17
ignore_all : bool = False
18
18
is_directory : bool = False
19
19
20
20
21
+ class CompileWarning (TypedDict ):
22
+ file : str
23
+ line : str
24
+ column : str
25
+ message : str
26
+ option : str
27
+
28
+
21
29
def parse_warning_ignore_file (file_path : str ) -> set [IgnoreRule ]:
22
30
"""
23
31
Parses the warning ignore file and returns a set of IgnoreRules
24
32
"""
25
- files_with_expected_warnings = set ()
33
+ files_with_expected_warnings : set [ IgnoreRule ] = set ()
26
34
with Path (file_path ).open (encoding = "UTF-8" ) as ignore_rules_file :
27
35
files_with_expected_warnings = set ()
28
36
for i , line in enumerate (ignore_rules_file ):
@@ -46,7 +54,7 @@ def parse_warning_ignore_file(file_path: str) -> set[IgnoreRule]:
46
54
)
47
55
sys .exit (1 )
48
56
if ignore_all :
49
- count = 0
57
+ count = "0"
50
58
51
59
files_with_expected_warnings .add (
52
60
IgnoreRule (
@@ -61,7 +69,7 @@ def extract_warnings_from_compiler_output(
61
69
compiler_output : str ,
62
70
compiler_output_type : str ,
63
71
path_prefix : str = "" ,
64
- ) -> list [dict ]:
72
+ ) -> list [CompileWarning ]:
65
73
"""
66
74
Extracts warnings from the compiler output based on compiler
67
75
output type. Removes path prefix from file paths if provided.
@@ -78,8 +86,12 @@ def extract_warnings_from_compiler_output(
78
86
r"(?P<file>.*):(?P<line>\d+):(?P<column>\d+): warning: "
79
87
r"(?P<message>.*) (?P<option>\[-[^\]]+\])$"
80
88
)
89
+ else :
90
+ raise RuntimeError (
91
+ f"Unsupported compiler output type: { compiler_output_type } " ,
92
+ )
81
93
compiled_regex = re .compile (regex_pattern )
82
- compiler_warnings = []
94
+ compiler_warnings : list [ CompileWarning ] = []
83
95
for i , line in enumerate (compiler_output .splitlines (), start = 1 ):
84
96
if match := compiled_regex .match (line ):
85
97
try :
@@ -100,7 +112,9 @@ def extract_warnings_from_compiler_output(
100
112
return compiler_warnings
101
113
102
114
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 ]]:
104
118
"""
105
119
Returns a dictionary where the key is the file and the data is the
106
120
warnings in that file. Does not include duplicate warnings for a
@@ -138,7 +152,7 @@ def is_file_ignored(
138
152
139
153
def get_unexpected_warnings (
140
154
ignore_rules : set [IgnoreRule ],
141
- files_with_warnings : set [ IgnoreRule ],
155
+ files_with_warnings : dict [ str , list [ CompileWarning ] ],
142
156
) -> int :
143
157
"""
144
158
Returns failure status if warnings discovered in list of warnings
@@ -180,7 +194,7 @@ def get_unexpected_warnings(
180
194
181
195
def get_unexpected_improvements (
182
196
ignore_rules : set [IgnoreRule ],
183
- files_with_warnings : set [ IgnoreRule ],
197
+ files_with_warnings : dict [ str , list [ CompileWarning ] ],
184
198
) -> int :
185
199
"""
186
200
Returns failure status if the number of warnings for a file is greater
0 commit comments