@@ -107,7 +107,7 @@ def get_para_line_numbers(file_obj: TextIO) -> list[list[int]]:
107
107
return paragraphs
108
108
109
109
110
- def parse_and_filter_warnings (
110
+ def filter_and_parse_warnings (
111
111
warnings : list [str ], files : set [Path ]
112
112
) -> list [re .Match [str ]]:
113
113
"""Get the warnings matching passed files and parse them with regex."""
@@ -124,6 +124,30 @@ def parse_and_filter_warnings(
124
124
return non_null_matches
125
125
126
126
127
+ def filter_warnings_by_diff (
128
+ warnings : list [re .Match [str ]], ref_a : str , ref_b : str , file : Path
129
+ ) -> list [re .Match [str ]]:
130
+ """Filter the passed per-file warnings to just those on changed lines."""
131
+ diff_lines = get_diff_lines (ref_a , ref_b , file )
132
+ with file .open (encoding = "UTF-8" ) as file_obj :
133
+ paragraphs = get_para_line_numbers (file_obj )
134
+ touched_paras = [
135
+ para_lines
136
+ for para_lines in paragraphs
137
+ if set (diff_lines ) & set (para_lines )
138
+ ]
139
+ touched_para_lines = set (itertools .chain (* touched_paras ))
140
+ warnings_infile = [
141
+ warning for warning in warnings if str (file ) in warning ["file" ]
142
+ ]
143
+ warnings_touched = [
144
+ warning
145
+ for warning in warnings_infile
146
+ if int (warning ["line" ]) in touched_para_lines
147
+ ]
148
+ return warnings_touched
149
+
150
+
127
151
def process_touched_warnings (
128
152
warnings : list [str ], ref_a : str , ref_b : str
129
153
) -> list [re .Match [str ]]:
@@ -132,36 +156,22 @@ def process_touched_warnings(
132
156
get_diff_files (ref_a , ref_b , filter_mode = mode ) for mode in ("A" , "M" )
133
157
)
134
158
135
- warnings_added = parse_and_filter_warnings (warnings , added_files )
136
- warnings_modified = parse_and_filter_warnings (warnings , modified_files )
159
+ warnings_added = filter_and_parse_warnings (warnings , added_files )
160
+ warnings_modified = filter_and_parse_warnings (warnings , modified_files )
137
161
138
162
modified_files_warned = {
139
163
file
140
164
for file in modified_files
141
165
if any (str (file ) in warning ["file" ] for warning in warnings_modified )
142
166
}
143
167
144
- warnings_touched = warnings_added .copy ()
145
- for file in modified_files_warned :
146
- diff_lines = get_diff_lines (ref_a , ref_b , file )
147
- with file .open (encoding = "UTF-8" ) as file_obj :
148
- paragraphs = get_para_line_numbers (file_obj )
149
- touched_paras = [
150
- para_lines
151
- for para_lines in paragraphs
152
- if set (diff_lines ) & set (para_lines )
153
- ]
154
- touched_para_lines = set (itertools .chain (* touched_paras ))
155
- warnings_infile = [
156
- warning
157
- for warning in warnings_modified
158
- if str (file ) in warning ["file" ]
159
- ]
160
- warnings_touched += [
161
- warning
162
- for warning in warnings_infile
163
- if int (warning ["line" ]) in touched_para_lines
164
- ]
168
+ warnings_modified_touched = [
169
+ filter_warnings_by_diff (warnings_modified , ref_a , ref_b , file )
170
+ for file in modified_files_warned
171
+ ]
172
+ warnings_touched = warnings_added + list (
173
+ itertools .chain (* warnings_modified_touched )
174
+ )
165
175
166
176
return warnings_touched
167
177
0 commit comments