mach: Enable ANN rules (type annotations) for ruff Python linter (#38531)

This changes will introduce [flake8-annotations
(ANN)](https://docs.astral.sh/ruff/rules/#flake8-annotations-ann) for
python type annotation, this will make all thing related to function
strictly typed in python

This rule will start to affected this directory from now:
- /python -> Root directory
- /python/tidy
- /python/wpt

Testing: `./mach test-tidy`
Fixes: Not related to any issues

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
This commit is contained in:
Jerens Lensun 2025-08-14 18:36:17 +08:00 committed by GitHub
parent 9c1ee4be83
commit 797db25c4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 122 additions and 70 deletions

View file

@ -56,7 +56,7 @@ class LocalGitRepo:
# git in advance and run the subprocess by its absolute path.
self.git_path = shutil.which("git")
def run_without_encoding(self, *args, env: dict = {}) -> bytes:
def run_without_encoding(self, *args: str, env: dict = {}) -> bytes:
if self.git_path is None:
raise RuntimeError("Git executable not found in PATH")
command_line = [self.git_path] + list(args)
@ -75,7 +75,7 @@ class LocalGitRepo:
)
raise exception
def run(self, *args, env: dict = {}) -> str:
def run(self, *args: str, env: dict = {}) -> str:
return self.run_without_encoding(*args, env=env).decode("utf-8", errors="surrogateescape")
@ -164,7 +164,7 @@ class WPTSync:
self.local_servo_repo = LocalGitRepo(self.servo_path, self)
self.local_wpt_repo = LocalGitRepo(self.wpt_path, self)
def run(self, payload: dict, step_callback=None) -> bool:
def run(self, payload: dict, step_callback: Callable[[Step], None] | None = None) -> bool:
if "pull_request" not in payload:
return True
@ -184,7 +184,7 @@ class WPTSync:
try:
servo_pr = self.servo.get_pull_request(pull_data["number"])
downstream_wpt_branch = self.downstream_wpt.get_branch(
wpt_branch_name_from_servo_pr_number(servo_pr.number)
wpt_branch_name_from_servo_pr_number(str(servo_pr.number))
)
upstream_pr = self.wpt.get_open_pull_request_for_branch(self.github_username, downstream_wpt_branch)
if upstream_pr:

View file

@ -43,5 +43,5 @@ COULD_NOT_MERGE_CHANGES_UPSTREAM_COMMENT = (
)
def wpt_branch_name_from_servo_pr_number(servo_pr_number) -> str:
def wpt_branch_name_from_servo_pr_number(servo_pr_number: str) -> str:
return f"servo_export_{servo_pr_number}"

View file

@ -18,7 +18,7 @@ from __future__ import annotations
import logging
import urllib.parse
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Any
import requests
@ -29,7 +29,7 @@ USER_AGENT = "Servo web-platform-test sync service"
TIMEOUT = 30 # 30 seconds
def authenticated(sync: WPTSync, method: str, url: str, json=None) -> requests.Response:
def authenticated(sync: WPTSync, method: str, url: str, json: dict[str, Any] | None = None) -> requests.Response:
logging.info(" → Request: %s %s", method, url)
if json:
logging.info(" → Request JSON: %s", json)
@ -138,7 +138,7 @@ class PullRequest:
def __str__(self) -> str:
return f"{self.repo}#{self.number}"
def api(self, *args, **kwargs) -> requests.Response:
def api(self, *args: Any, **kwargs: dict[str, Any]) -> requests.Response:
return authenticated(self.context, *args, **kwargs)
def leave_comment(self, comment: str) -> requests.Response:
@ -163,7 +163,8 @@ class PullRequest:
self.api("DELETE", f"{self.base_issues_url}/labels/{label}")
def add_labels(self, labels: list[str]) -> None:
self.api("POST", f"{self.base_issues_url}/labels", json=labels)
data = {"labels": labels}
self.api("POST", f"{self.base_issues_url}/labels", json=data)
def merge(self) -> None:
self.api("PUT", f"{self.base_url}/merge", json={"merge_method": "rebase"})

View file

@ -19,7 +19,7 @@ import logging
import os
import textwrap
from typing import TYPE_CHECKING, Generic, Optional, TypeVar
from typing import TYPE_CHECKING, Generic, Optional, TypeVar, Callable, Any
from .common import COULD_NOT_APPLY_CHANGES_DOWNSTREAM_COMMENT
from .common import COULD_NOT_APPLY_CHANGES_UPSTREAM_COMMENT
@ -36,7 +36,7 @@ PATCH_FILE_NAME = "tmp.patch"
class Step:
def __init__(self, name) -> None:
def __init__(self, name: str) -> None:
self.name = name
def provides(self) -> Optional[AsyncValue]:
@ -91,7 +91,9 @@ class CreateOrUpdateBranchForPRStep(Step):
if run.upstream_pr.has_value():
run.add_step(CommentStep(run.upstream_pr.value(), COULD_NOT_APPLY_CHANGES_UPSTREAM_COMMENT))
def _get_upstreamable_commits_from_local_servo_repo(self, sync: WPTSync):
def _get_upstreamable_commits_from_local_servo_repo(
self, sync: WPTSync
) -> list[dict[str, bytes | str] | dict[str, str]]:
local_servo_repo = sync.local_servo_repo
number_of_commits = self.pull_data["commits"]
pr_head = self.pull_data["head"]["sha"]
@ -143,7 +145,9 @@ class CreateOrUpdateBranchForPRStep(Step):
run.sync.local_wpt_repo.run("add", "--all")
run.sync.local_wpt_repo.run("commit", "--message", commit["message"], "--author", commit["author"])
def _create_or_update_branch_for_pr(self, run: SyncRun, commits: list[dict], pre_commit_callback=None) -> str:
def _create_or_update_branch_for_pr(
self, run: SyncRun, commits: list[dict], pre_commit_callback: Callable[[], None] | None = None
) -> str:
branch_name = wpt_branch_name_from_servo_pr_number(self.pull_data["number"])
try:
# Create a new branch with a unique name that is consistent between
@ -180,7 +184,7 @@ class CreateOrUpdateBranchForPRStep(Step):
class RemoveBranchForPRStep(Step):
def __init__(self, pull_request) -> None:
def __init__(self, pull_request: dict[str, Any]) -> None:
Step.__init__(self, "RemoveBranchForPRStep")
self.branch_name = wpt_branch_name_from_servo_pr_number(pull_request["number"])