Skip to content

[Workflow] make code-format-helper.py mypy-safe (NFC) #69691

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 2 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions llvm/utils/git/code-format-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@

class FormatHelper:
COMMENT_TAG = "<!--LLVM CODE FORMAT COMMENT: {fmt}-->"
name = "unknown"
name: str
friendly_name: str

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

def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
pass
@property
def instructions(self) -> str:
raise NotImplementedError()

def format_run(
self, changed_files: list[str], args: argparse.Namespace
) -> str | None:
raise NotImplementedError()

def pr_comment_text(self, diff: str) -> str:
return f"""
Expand Down Expand Up @@ -66,7 +73,7 @@ def find_comment(
return comment
return None

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

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

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

Expand All @@ -91,7 +98,7 @@ def update_pr_success(self, args: argparse.Namespace):
"""
)

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

@property
def instructions(self):
def instructions(self) -> str:
return " ".join(self.cf_cmd)

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

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

def filter_changed_files(self, changed_files: [str]) -> [str]:
def filter_changed_files(self, changed_files: list[str]) -> list[str]:
filtered_files = []
for path in changed_files:
_, ext = os.path.splitext(path)
Expand All @@ -129,10 +136,12 @@ def filter_changed_files(self, changed_files: [str]) -> [str]:
filtered_files.append(path)
return filtered_files

def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
def format_run(
self, changed_files: list[str], args: argparse.Namespace
) -> str | None:
cpp_files = self.filter_changed_files(changed_files)
if not cpp_files:
return
return None
cf_cmd = [
"git-clang-format",
"--diff",
Expand All @@ -156,10 +165,10 @@ class DarkerFormatHelper(FormatHelper):
friendly_name = "Python code formatter"

@property
def instructions(self):
def instructions(self) -> str:
return " ".join(self.darker_cmd)

def filter_changed_files(self, changed_files: [str]) -> [str]:
def filter_changed_files(self, changed_files: list[str]) -> list[str]:
filtered_files = []
for path in changed_files:
name, ext = os.path.splitext(path)
Expand All @@ -168,10 +177,12 @@ def filter_changed_files(self, changed_files: [str]) -> [str]:

return filtered_files

def format_run(self, changed_files: [str], args: argparse.Namespace) -> str | None:
def format_run(
self, changed_files: list[str], args: argparse.Namespace
) -> str | None:
py_files = self.filter_changed_files(changed_files)
if not py_files:
return
return None
darker_cmd = [
"darker",
"--check",
Expand Down