Skip to content

Commit 3b3d1a5

Browse files
authored
[NFC][clang-tidy] Add type annotations to check_clang_tidy (#133140)
``` > python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py Success: no issues found in 1 source file ```
1 parent 92e5916 commit 3b3d1a5

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

clang-tools-extra/test/clang-tidy/check_clang_tidy.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@
4848
import re
4949
import subprocess
5050
import sys
51+
from typing import List, Tuple
5152

5253

53-
def write_file(file_name, text):
54+
def write_file(file_name: str, text: str) -> None:
5455
with open(file_name, "w", encoding="utf-8") as f:
5556
f.write(text)
5657
f.truncate()
5758

5859

59-
def try_run(args, raise_error=True):
60+
def try_run(args: List[str], raise_error: bool = True) -> str:
6061
try:
6162
process_output = subprocess.check_output(args, stderr=subprocess.STDOUT).decode(
6263
errors="ignore"
@@ -71,12 +72,12 @@ def try_run(args, raise_error=True):
7172

7273
# This class represents the appearance of a message prefix in a file.
7374
class MessagePrefix:
74-
def __init__(self, label):
75+
def __init__(self, label: str) -> None:
7576
self.has_message = False
76-
self.prefixes = []
77+
self.prefixes: List[str] = []
7778
self.label = label
7879

79-
def check(self, file_check_suffix, input_text):
80+
def check(self, file_check_suffix: str, input_text: str) -> bool:
8081
self.prefix = self.label + file_check_suffix
8182
self.has_message = self.prefix in input_text
8283
if self.has_message:
@@ -85,7 +86,7 @@ def check(self, file_check_suffix, input_text):
8586

8687

8788
class CheckRunner:
88-
def __init__(self, args, extra_args):
89+
def __init__(self, args: argparse.Namespace, extra_args: List[str]) -> None:
8990
self.resource_dir = args.resource_dir
9091
self.assume_file_name = args.assume_filename
9192
self.input_file_name = args.input_file_name
@@ -143,11 +144,11 @@ def __init__(self, args, extra_args):
143144
if self.resource_dir is not None:
144145
self.clang_extra_args.append("-resource-dir=%s" % self.resource_dir)
145146

146-
def read_input(self):
147+
def read_input(self) -> None:
147148
with open(self.input_file_name, "r", encoding="utf-8") as input_file:
148149
self.input_text = input_file.read()
149150

150-
def get_prefixes(self):
151+
def get_prefixes(self) -> None:
151152
for suffix in self.check_suffix:
152153
if suffix and not re.match("^[A-Z0-9\\-]+$", suffix):
153154
sys.exit(
@@ -189,7 +190,7 @@ def get_prefixes(self):
189190
)
190191
assert expect_diagnosis or self.expect_no_diagnosis
191192

192-
def prepare_test_inputs(self):
193+
def prepare_test_inputs(self) -> None:
193194
# Remove the contents of the CHECK lines to avoid CHECKs matching on
194195
# themselves. We need to keep the comments to preserve line numbers while
195196
# avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +199,7 @@ def prepare_test_inputs(self):
198199
write_file(self.temp_file_name, cleaned_test)
199200
write_file(self.original_file_name, cleaned_test)
200201

201-
def run_clang_tidy(self):
202+
def run_clang_tidy(self) -> str:
202203
args = (
203204
[
204205
"clang-tidy",
@@ -238,11 +239,11 @@ def run_clang_tidy(self):
238239
print("------------------------------------------------------------------")
239240
return clang_tidy_output
240241

241-
def check_no_diagnosis(self, clang_tidy_output):
242+
def check_no_diagnosis(self, clang_tidy_output: str) -> None:
242243
if clang_tidy_output != "":
243244
sys.exit("No diagnostics were expected, but found the ones above")
244245

245-
def check_fixes(self):
246+
def check_fixes(self) -> None:
246247
if self.has_check_fixes:
247248
try_run(
248249
[
@@ -254,7 +255,7 @@ def check_fixes(self):
254255
]
255256
)
256257

257-
def check_messages(self, clang_tidy_output):
258+
def check_messages(self, clang_tidy_output: str) -> None:
258259
if self.has_check_messages:
259260
messages_file = self.temp_file_name + ".msg"
260261
write_file(messages_file, clang_tidy_output)
@@ -268,7 +269,7 @@ def check_messages(self, clang_tidy_output):
268269
]
269270
)
270271

271-
def check_notes(self, clang_tidy_output):
272+
def check_notes(self, clang_tidy_output: str) -> None:
272273
if self.has_check_notes:
273274
notes_file = self.temp_file_name + ".notes"
274275
filtered_output = [
@@ -287,7 +288,7 @@ def check_notes(self, clang_tidy_output):
287288
]
288289
)
289290

290-
def run(self):
291+
def run(self) -> None:
291292
self.read_input()
292293
if self.export_fixes is None:
293294
self.get_prefixes()
@@ -313,7 +314,7 @@ def run(self):
313314
C_STANDARDS = ["c99", ("c11", "c1x"), "c17", ("c23", "c2x"), "c2y"]
314315

315316

316-
def expand_std(std):
317+
def expand_std(std: str) -> List[str]:
317318
split_std, or_later, _ = std.partition("-or-later")
318319

319320
if not or_later:
@@ -335,11 +336,11 @@ def expand_std(std):
335336
return [std]
336337

337338

338-
def csv(string):
339+
def csv(string: str) -> List[str]:
339340
return string.split(",")
340341

341342

342-
def parse_arguments():
343+
def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
343344
parser = argparse.ArgumentParser(
344345
prog=pathlib.Path(__file__).stem,
345346
description=__doc__,
@@ -374,7 +375,7 @@ def parse_arguments():
374375
return parser.parse_known_args()
375376

376377

377-
def main():
378+
def main() -> None:
378379
args, extra_args = parse_arguments()
379380

380381
abbreviated_stds = args.std

0 commit comments

Comments
 (0)