[torchgen] Fix ruff format for # fmt: skip comment for function signature (#155909)

See also:

- astral-sh/ruff#18658

This fix follows the suggestion from:

- https://github.com/astral-sh/ruff/issues/18658#issuecomment-2970130276

Pull Request resolved: https://github.com/pytorch/pytorch/pull/155909
Approved by: https://github.com/ezyang
This commit is contained in:
Xuehai Pan
2025-06-14 15:05:28 +08:00
committed by PyTorch MergeBot
parent d859e65826
commit 736a15a81a

View File

@ -212,17 +212,17 @@ def format_function_signature(
if len(sig) <= 80 or len(arguments) == 0 or tuple(arguments) == ("self",):
return sig
arguments = [f" {arg}," for arg in arguments]
return "\n".join(
(
lines = [
f"def {name}(",
*(
arg if len(arg) <= 80 else f" # fmt: off\n{arg}\n # fmt: on"
for arg in arguments
),
*(f" {arg}," for arg in arguments),
f"){return_type}: ...",
)
).replace(" # fmt: off\n # fmt: on\n", "")
]
sig = "\n".join(lines)
if all(len(line) <= 80 for line in lines):
return sig
# ruff format bug for compound statements: https://github.com/astral-sh/ruff/issues/18658
# use `skip` instead of `on` + `off`
return sig.removesuffix(" ...") + " # fmt: skip\n ..."
@dataclass(frozen=True)