Skip to content

Commit 04377a1

Browse files
authored
gh-133403: Run mypy on Tools/build/check_warnings.py (#137700)
1 parent c87b66b commit 04377a1

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

.github/workflows/mypy.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ on:
1313
- "Lib/test/libregrtest/**"
1414
- "Lib/tomllib/**"
1515
- "Misc/mypy/**"
16-
- "Tools/build/mypy.ini"
1716
- "Tools/build/check_extension_modules.py"
17+
- "Tools/build/check_warnings.py"
1818
- "Tools/build/compute-changes.py"
1919
- "Tools/build/deepfreeze.py"
20+
- "Tools/build/generate-build-details.py"
2021
- "Tools/build/generate_sbom.py"
2122
- "Tools/build/generate_stdlib_module_names.py"
22-
- "Tools/build/generate-build-details.py"
23-
- "Tools/build/verify_ensurepip_wheels.py"
24-
- "Tools/build/update_file.py"
23+
- "Tools/build/mypy.ini"
2524
- "Tools/build/umarshal.py"
25+
- "Tools/build/update_file.py"
26+
- "Tools/build/verify_ensurepip_wheels.py"
2627
- "Tools/cases_generator/**"
2728
- "Tools/clinic/**"
2829
- "Tools/jit/**"

Tools/build/check_warnings.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@
88
import sys
99
from collections import defaultdict
1010
from pathlib import Path
11-
from typing import NamedTuple
11+
from typing import NamedTuple, TypedDict
1212

1313

1414
class 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+
2129
def 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

139153
def 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

181195
def 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

Tools/build/mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# .github/workflows/mypy.yml
55
files =
66
Tools/build/check_extension_modules.py,
7+
Tools/build/check_warnings.py,
78
Tools/build/compute-changes.py,
89
Tools/build/deepfreeze.py,
910
Tools/build/generate-build-details.py,

0 commit comments

Comments
 (0)