Skip to content

[NFC][clang-tidy] Add type annotations to check_clang_tidy #133140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025

Conversation

nicovank
Copy link
Contributor

I'm looking to make some changes in this file, start by typing it.

> python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Success: no issues found in 1 source file

@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Nicolas van Kempen (nicovank)

Changes

I'm looking to make some changes in this file, start by typing it.

> python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Success: no issues found in 1 source file

Full diff: https://github.com/llvm/llvm-project/pull/133140.diff

1 Files Affected:

  • (modified) clang-tools-extra/test/clang-tidy/check_clang_tidy.py (+20-19)
diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
index 5e39c05f76d86..e148b67dd15b7 100755
--- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
+++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
@@ -48,15 +48,16 @@
 import re
 import subprocess
 import sys
+from typing import List, Tuple
 
 
-def write_file(file_name, text):
+def write_file(file_name: str, text: str) -> None:
     with open(file_name, "w", encoding="utf-8") as f:
         f.write(text)
         f.truncate()
 
 
-def try_run(args, raise_error=True):
+def try_run(args: List[str], raise_error: bool = True) -> str:
     try:
         process_output = subprocess.check_output(args, stderr=subprocess.STDOUT).decode(
             errors="ignore"
@@ -71,12 +72,12 @@ def try_run(args, raise_error=True):
 
 # This class represents the appearance of a message prefix in a file.
 class MessagePrefix:
-    def __init__(self, label):
+    def __init__(self, label: str):
         self.has_message = False
-        self.prefixes = []
+        self.prefixes: List[str] = []
         self.label = label
 
-    def check(self, file_check_suffix, input_text):
+    def check(self, file_check_suffix: str, input_text: str) -> bool:
         self.prefix = self.label + file_check_suffix
         self.has_message = self.prefix in input_text
         if self.has_message:
@@ -85,7 +86,7 @@ def check(self, file_check_suffix, input_text):
 
 
 class CheckRunner:
-    def __init__(self, args, extra_args):
+    def __init__(self, args: argparse.Namespace, extra_args: List[str]):
         self.resource_dir = args.resource_dir
         self.assume_file_name = args.assume_filename
         self.input_file_name = args.input_file_name
@@ -143,11 +144,11 @@ def __init__(self, args, extra_args):
         if self.resource_dir is not None:
             self.clang_extra_args.append("-resource-dir=%s" % self.resource_dir)
 
-    def read_input(self):
+    def read_input(self) -> None:
         with open(self.input_file_name, "r", encoding="utf-8") as input_file:
             self.input_text = input_file.read()
 
-    def get_prefixes(self):
+    def get_prefixes(self) -> None:
         for suffix in self.check_suffix:
             if suffix and not re.match("^[A-Z0-9\\-]+$", suffix):
                 sys.exit(
@@ -189,7 +190,7 @@ def get_prefixes(self):
             )
         assert expect_diagnosis or self.expect_no_diagnosis
 
-    def prepare_test_inputs(self):
+    def prepare_test_inputs(self) -> None:
         # Remove the contents of the CHECK lines to avoid CHECKs matching on
         # themselves.  We need to keep the comments to preserve line numbers while
         # avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +199,7 @@ def prepare_test_inputs(self):
         write_file(self.temp_file_name, cleaned_test)
         write_file(self.original_file_name, cleaned_test)
 
-    def run_clang_tidy(self):
+    def run_clang_tidy(self) -> str:
         args = (
             [
                 "clang-tidy",
@@ -238,11 +239,11 @@ def run_clang_tidy(self):
         print("------------------------------------------------------------------")
         return clang_tidy_output
 
-    def check_no_diagnosis(self, clang_tidy_output):
+    def check_no_diagnosis(self, clang_tidy_output: str) -> None:
         if clang_tidy_output != "":
             sys.exit("No diagnostics were expected, but found the ones above")
 
-    def check_fixes(self):
+    def check_fixes(self) -> None:
         if self.has_check_fixes:
             try_run(
                 [
@@ -254,7 +255,7 @@ def check_fixes(self):
                 ]
             )
 
-    def check_messages(self, clang_tidy_output):
+    def check_messages(self, clang_tidy_output: str) -> None:
         if self.has_check_messages:
             messages_file = self.temp_file_name + ".msg"
             write_file(messages_file, clang_tidy_output)
@@ -268,7 +269,7 @@ def check_messages(self, clang_tidy_output):
                 ]
             )
 
-    def check_notes(self, clang_tidy_output):
+    def check_notes(self, clang_tidy_output: str) -> None:
         if self.has_check_notes:
             notes_file = self.temp_file_name + ".notes"
             filtered_output = [
@@ -287,7 +288,7 @@ def check_notes(self, clang_tidy_output):
                 ]
             )
 
-    def run(self):
+    def run(self) -> None:
         self.read_input()
         if self.export_fixes is None:
             self.get_prefixes()
@@ -313,7 +314,7 @@ def run(self):
 C_STANDARDS = ["c99", ("c11", "c1x"), "c17", ("c23", "c2x"), "c2y"]
 
 
-def expand_std(std):
+def expand_std(std: str) -> List[str]:
     split_std, or_later, _ = std.partition("-or-later")
 
     if not or_later:
@@ -335,11 +336,11 @@ def expand_std(std):
     return [std]
 
 
-def csv(string):
+def csv(string: str) -> List[str]:
     return string.split(",")
 
 
-def parse_arguments():
+def parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
     parser = argparse.ArgumentParser(
         prog=pathlib.Path(__file__).stem,
         description=__doc__,
@@ -374,7 +375,7 @@ def parse_arguments():
     return parser.parse_known_args()
 
 
-def main():
+def main() -> None:
     args, extra_args = parse_arguments()
 
     abbreviated_stds = args.std

Copy link

github-actions bot commented Mar 27, 2025

✅ With the latest revision this PR passed the Python code formatter.

I'm looking to make some changes in this file, start by typing it.

```
> python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Success: no issues found in 1 source file
```
@nicovank nicovank merged commit 3b3d1a5 into llvm:main Mar 30, 2025
13 of 14 checks passed
@nicovank nicovank deleted the pr133140 branch March 30, 2025 22:48
@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 31, 2025

LLVM Buildbot has detected a new failure on builder flang-runtime-cuda-clang running on as-builder-7 while building clang-tools-extra at step 7 "build-flang-default".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/7/builds/12917

Here is the relevant piece of the build log for the reference
Step 7 (build-flang-default) failure: cmake (failure)
...
92.613 [1554/130/6034] Building CXX object tools/mlir/lib/ExecutionEngine/CMakeFiles/MLIRExecutionEngine.dir/ExecutionEngine.cpp.o
92.669 [1553/130/6035] Building CXX object tools/mlir/test/lib/Dialect/Transform/CMakeFiles/MLIRTestTransformDialect.dir/TestTransformDialectInterpreter.cpp.o
92.740 [1552/130/6036] Building CXX object tools/mlir/lib/CAPI/Dialect/CMakeFiles/obj.MLIRCAPIAMDGPU.dir/AMDGPU.cpp.o
92.743 [1551/130/6037] Linking CXX static library lib/libMLIRTransformUtils.a
92.803 [1550/130/6038] Building CXX object tools/mlir/lib/CAPI/Dialect/CMakeFiles/obj.MLIRCAPIArith.dir/Arith.cpp.o
92.839 [1549/130/6039] Linking CXX static library lib/libMLIRMathToEmitC.a
92.969 [1548/130/6040] Linking CXX static library lib/libMLIRComplexToLibm.a
93.040 [1547/130/6041] Linking CXX static library lib/libMLIRControlFlowToSCF.a
93.145 [1546/130/6042] Linking CXX static library lib/libMLIRFuncToEmitC.a
93.157 [1545/130/6043] Linking CXX static library lib/libMLIRMathToLibm.a
command timed out: 1200 seconds without output running [b'cmake', b'--build', b'.'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=4050.115849

SchrodingerZhu pushed a commit to SchrodingerZhu/llvm-project that referenced this pull request Mar 31, 2025
```
> python3 -m mypy --strict clang-tools-extra/test/clang-tidy/check_clang_tidy.py
Success: no issues found in 1 source file
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants