Mach: add type check on python tidy folder (#38043)

Introduce `python/tidy` folder in pyrefly type checker

Testing: Manual testing via `./mach test-tidy` command

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Jerens Lensun 2025-07-17 15:35:11 +08:00 committed by GitHub
parent 2ad250de26
commit 1c4797809a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 137 additions and 90 deletions

View file

@ -8,7 +8,7 @@
# except according to those terms.
from dataclasses import dataclass
from typing import Any, Literal, NotRequired
from typing import Any, Literal, Optional
@dataclass
@ -19,20 +19,20 @@ class GithubAnnotation:
level: Literal["notice", "warning", "error"]
title: str
message: str
column_start: NotRequired[int]
column_end: NotRequired[int]
column_start: Optional[int] = None
column_end: Optional[int] = None
class GitHubAnnotationManager:
def __init__(self, annotation_prefix: str, limit: int = 10):
def __init__(self, annotation_prefix: str, limit: int = 10) -> None:
self.annotation_prefix: str = annotation_prefix
self.limit: int = limit
self.total_count: int = 0
def clean_path(self, path: str):
def clean_path(self, path: str) -> str:
return path.removeprefix("./")
def escape(self, s: str):
def escape(self, s: str) -> str:
return s.replace("\r", "%0D").replace("\n", "%0A")
def emit_annotation(
@ -42,42 +42,38 @@ class GitHubAnnotationManager:
file_name: str,
line_start: int,
line_end: int | None = None,
annotation_level: str = "error",
annotation_level: Literal["notice", "warning", "error"] = "error",
column_start: int | None = None,
column_end: int | None = None,
):
) -> None:
if self.total_count >= self.limit:
return
if line_end is None:
line_end = line_start
annotation: GithubAnnotation = {
"title": f"{self.annotation_prefix}: {self.escape(title)}",
"message": self.escape(message),
"file_name": self.clean_path(file_name),
"line_start": line_start,
"line_end": line_end,
"level": annotation_level,
}
annotation = GithubAnnotation(
title=f"{self.annotation_prefix}: {self.escape(title)}",
message=self.escape(message),
file_name=self.clean_path(file_name),
line_start=line_start,
line_end=line_end,
level=annotation_level,
column_start=column_start,
column_end=column_end,
)
if line_start == line_end and column_start is not None and column_end is not None:
annotation["column_start"] = column_start
annotation["column_end"] = column_end
line_info = f"line={annotation['line_start']},endLine={annotation['line_end']},title={annotation['title']}"
line_info = f"line={annotation.line_start},endLine={annotation.line_end},title={annotation.title}"
column_info = ""
if "column_end" in annotation and "column_start" in annotation:
column_info = f"col={annotation['column_start']},endColumn={annotation['column_end']},"
if line_start == line_end and annotation.column_end is not None and annotation.column_start is not None:
column_info = f"col={annotation.column_start},endColumn={annotation.column_end},"
print(
f"::{annotation['level']} file={annotation['file_name']},{column_info}{line_info}::{annotation['message']}"
)
print(f"::{annotation.level} file={annotation.file_name},{column_info}{line_info}::{annotation.message}")
self.total_count += 1
def emit_annotations_for_clippy(self, data: list[dict[str, Any]]):
def emit_annotations_for_clippy(self, data: list[dict[str, Any]]) -> None:
severenty_map: dict[str, Literal["notice", "warning", "error"]] = {
"help": "notice",
"note": "notice",