Skip to content

Commit af25304

Browse files
authored
[Workflow] make code-format-helper.py mypy-safe (NFC) (#69691)
Fix type errors that mypy reports with code-format-helper.py. Add a few return type annotations and change `param: [str]` to `param: list[str]`. Leave a few required FormatHelper members missing instead of defining a placeholder: - FormatHelper.name - FormatHelper.friendly_name - FormatHelper.format_run: NotImplementedError() instead of `pass`
1 parent a7119a1 commit af25304

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

llvm/utils/git/code-format-helper.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@
2020

2121
class FormatHelper:
2222
COMMENT_TAG = "<!--LLVM CODE FORMAT COMMENT: {fmt}-->"
23-
name = "unknown"
23+
name: str
24+
friendly_name: str
2425

2526
@property
2627
def comment_tag(self) -> str:
2728
return self.COMMENT_TAG.replace("fmt", self.name)
2829

29-
def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
30-
pass
30+
@property
31+
def instructions(self) -> str:
32+
raise NotImplementedError()
33+
34+
def format_run(
35+
self, changed_files: list[str], args: argparse.Namespace
36+
) -> str | None:
37+
raise NotImplementedError()
3138

3239
def pr_comment_text(self, diff: str) -> str:
3340
return f"""
@@ -66,7 +73,7 @@ def find_comment(
6673
return comment
6774
return None
6875

69-
def update_pr(self, diff: str, args: argparse.Namespace):
76+
def update_pr(self, diff: str, args: argparse.Namespace) -> None:
7077
repo = github.Github(args.token).get_repo(args.repo)
7178
pr = repo.get_issue(args.issue_number).as_pull_request()
7279

@@ -78,7 +85,7 @@ def update_pr(self, diff: str, args: argparse.Namespace):
7885
else:
7986
pr.as_issue().create_comment(pr_text)
8087

81-
def update_pr_success(self, args: argparse.Namespace):
88+
def update_pr_success(self, args: argparse.Namespace) -> None:
8289
repo = github.Github(args.token).get_repo(args.repo)
8390
pr = repo.get_issue(args.issue_number).as_pull_request()
8491

@@ -91,7 +98,7 @@ def update_pr_success(self, args: argparse.Namespace):
9198
"""
9299
)
93100

94-
def run(self, changed_files: [str], args: argparse.Namespace):
101+
def run(self, changed_files: list[str], args: argparse.Namespace) -> bool:
95102
diff = self.format_run(changed_files, args)
96103
if diff:
97104
self.update_pr(diff, args)
@@ -106,11 +113,11 @@ class ClangFormatHelper(FormatHelper):
106113
friendly_name = "C/C++ code formatter"
107114

108115
@property
109-
def instructions(self):
116+
def instructions(self) -> str:
110117
return " ".join(self.cf_cmd)
111118

112119
@cached_property
113-
def libcxx_excluded_files(self):
120+
def libcxx_excluded_files(self) -> list[str]:
114121
with open("libcxx/utils/data/ignore_format.txt", "r") as ifd:
115122
return [excl.strip() for excl in ifd.readlines()]
116123

@@ -120,7 +127,7 @@ def should_be_excluded(self, path: str) -> bool:
120127
return True
121128
return False
122129

123-
def filter_changed_files(self, changed_files: [str]) -> [str]:
130+
def filter_changed_files(self, changed_files: list[str]) -> list[str]:
124131
filtered_files = []
125132
for path in changed_files:
126133
_, ext = os.path.splitext(path)
@@ -129,10 +136,12 @@ def filter_changed_files(self, changed_files: [str]) -> [str]:
129136
filtered_files.append(path)
130137
return filtered_files
131138

132-
def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
139+
def format_run(
140+
self, changed_files: list[str], args: argparse.Namespace
141+
) -> str | None:
133142
cpp_files = self.filter_changed_files(changed_files)
134143
if not cpp_files:
135-
return
144+
return None
136145
cf_cmd = [
137146
"git-clang-format",
138147
"--diff",
@@ -156,10 +165,10 @@ class DarkerFormatHelper(FormatHelper):
156165
friendly_name = "Python code formatter"
157166

158167
@property
159-
def instructions(self):
168+
def instructions(self) -> str:
160169
return " ".join(self.darker_cmd)
161170

162-
def filter_changed_files(self, changed_files: [str]) -> [str]:
171+
def filter_changed_files(self, changed_files: list[str]) -> list[str]:
163172
filtered_files = []
164173
for path in changed_files:
165174
name, ext = os.path.splitext(path)
@@ -168,10 +177,12 @@ def filter_changed_files(self, changed_files: [str]) -> [str]:
168177

169178
return filtered_files
170179

171-
def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
180+
def format_run(
181+
self, changed_files: list[str], args: argparse.Namespace
182+
) -> str | None:
172183
py_files = self.filter_changed_files(changed_files)
173184
if not py_files:
174-
return
185+
return None
175186
darker_cmd = [
176187
"darker",
177188
"--check",

0 commit comments

Comments
 (0)