8000 Refactor creating set of files with warnings to a dedicated function · python/cpython@bd1634e · GitHub
[go: up one dir, main page]

Skip to content

Commit bd1634e

Browse files
committed
Refactor creating set of files with warnings to a dedicated function
1 parent e2ca75f commit bd1634e

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Add tooling to check for changes in compiler warnings.
2+
Patch by Nate Ohlson

Tools/build/check_warnings.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Parses compiler output with -fdiagnostics-format=json and checks that warnings exist
44
only in files that are expected to have warnings.
55
"""
6-
import sys
7-
from pathlib import Path
86
import argparse
97
import json
108
import re
9+
import sys
10+
from pathlib import Path
1111

1212
def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]:
1313
"""
@@ -24,63 +24,67 @@ def extract_warnings_from_compiler_output(compiler_output: str) -> list[dict]:
2424
try:
2525
json_data = json.loads(array)
2626
json_objects_in_array = [entry for entry in json_data]
27-
compiler_warnings.extend([entry for entry in json_objects_in_array if entry.get('kind') == 'warning'])
27+
compiler_warnings.extend([entry for entry in json_objects_in_array
28+
if entry.get('kind') == 'warning'])
2829
except json.JSONDecodeError:
2930
continue # Skip malformed JSON
3031

3132
return compiler_warnings
3233

34+
# Create a function that returns a dictionary where the key is the file and the data is the warnings in that file
35+
def get_warnings_by_file(warnings: list[dict]) -> dict[str, list[dict]]:
36+
"""
37+
Returns a dictionary where the key is the file and the data is the warnings in that file
38+
"""
39+
warnings_by_file = {}
40+
for warning in warnings:
41+
locations = warning['locations']
42+
for location in locations:
43+
for key in ['caret', 'start', 'end']:
44+
if key in location:
45+
file = location[key]['file']
46+
file = file.lstrip('./') # Remove leading current directory if present
47+
if file not in warnings_by_file:
48+
warnings_by_file[file] = []
49+
warnings_by_file[file].append(warning)
50+
51+
return warnings_by_file
52+
3353
def get_unexpected_warnings(
3454
warnings: list[dict],
3555
files_with_expected_warnings: set[str],
56+
files_with_warnings: set[str],
3657
) -> int:
3758
"""
3859
Returns failure status if warnings discovered in list of warnings are associated with a file
3960
that is not found in the list of files with expected warnings
4061
"""
4162
unexpected_warnings = []
42-
for warning in warnings:
43-
locations = warning['locations']
44-
for location in locations:
45-
for key in ['caret', 'start', 'end']:
46-
if key in location:
47-
file = location[key]['file']
48-
file = file.lstrip('./') # Remove leading curdir if present
49-
if file not in files_with_expected_warnings:
50-
unexpected_warnings.append(warning)
63+
for file in files_with_warnings.keys():
64+
if file not in files_with_expected_warnings:
65+
unexpected_warnings.extend(files_with_warnings[file])
5166

5267
if unexpected_warnings:
5368
print("Unexpected warnings:")
5469
for warning in unexpected_warnings:
5570
print(warning)
5671
return 1
57-
72+
5873
return 0
5974

6075

6176
def get_unexpected_improvements(
6277
warnings: list[dict],
6378
files_with_expected_warnings: set[str],
79+
files_with_warnings: set[str],
6480
) -> int:
6581
"""
6682
Returns failure status if there are no warnings in the list of warnings for a file
6783
that is in the list of files with expected warnings
6884
"""
69-
70-
# Create set of files with warnings
71-
files_with_warnings = set()
72-
for warning in warnings:
73-
locations = warning['locations']
74-
for location in locations:
75-
for key in ['caret', 'start', 'end']:
76-
if key in location:
77-
file = location[key]['file']
78-
file = file.lstrip('./') # Remove leading curdir if present
79-
files_with_warnings.add(file)
80-
8185
unexpected_improvements = []
8286
for file in files_with_expected_warnings:
83-
if file not in files_with_warnings:
87+
if file not in files_with_warnings.keys():
8488
unexpected_improvements.append(file)
8589

8690
if unexpected_improvements:
@@ -143,11 +147,12 @@ def main(argv: list[str] | None = None) -> int:
143147
}
144148

145149
warnings = extract_warnings_from_compiler_output(compiler_output_file_contents)
150+
files_with_warnings = get_warnings_by_file(warnings)
146151

147-
status = get_unexpected_warnings(warnings, files_with_expected_warnings)
152+
status = get_unexpected_warnings(warnings, files_with_expected_warnings, files_with_warnings)
148153
if args.fail_on_regression: exit_code |= status
149154

150-
status = get_unexpected_improvements(warnings, files_with_expected_warnings)
155+
status = get_unexpected_improvements(warnings, files_with_expected_warnings, files_with_warnings)
151156
if args.fail_on_improvement: exit_code |= status
152157

153158
return exit_code

0 commit comments

Comments
 (0)
0