Skip to content

Commit 263c3c6

Browse files
committed
Factor filtering warnings by modified diffs into helper function
1 parent 3ee5f3e commit 263c3c6

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

Doc/tools/check-warnings.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get_para_line_numbers(file_obj: TextIO) -> list[list[int]]:
107107
return paragraphs
108108

109109

110-
def parse_and_filter_warnings(
110+
def filter_and_parse_warnings(
111111
warnings: list[str], files: set[Path]
112112
) -> list[re.Match[str]]:
113113
"""Get the warnings matching passed files and parse them with regex."""
@@ -124,6 +124,30 @@ def parse_and_filter_warnings(
124124
return non_null_matches
125125

126126

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+
127151
def process_touched_warnings(
128152
warnings: list[str], ref_a: str, ref_b: str
129153
) -> list[re.Match[str]]:
@@ -132,36 +156,22 @@ def process_touched_warnings(
132156
get_diff_files(ref_a, ref_b, filter_mode=mode) for mode in ("A", "M")
133157
)
134158

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)
137161

138162
modified_files_warned = {
139163
file
140164
for file in modified_files
141165
if any(str(file) in warning["file"] for warning in warnings_modified)
142166
}
143167

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+
)
165175

166176
return warnings_touched
167177

0 commit comments

Comments
 (0)