### What this PR does / why we need it?
This PR introduces several upstream `vllm`-aligned lint hooks into
`vllm-ascend` and makes them part of the actual `pre-commit` flow.
Main changes in this PR:
- add `check-boolean-context-manager` to catch boolean expressions in
`with` statements
- add `check-forbidden-imports` to forbid direct `re` imports and
disallowed direct `triton` imports
- enable shell script linting through `tools/shellcheck.sh`
- add root `.clang-format` aligned with upstream `vllm`, enable
`clang-format` in `pre-commit`, temporarily **exclude all `csrc/**`**
from `clang-format` to avoid bringing a large native code reformat into
this PR
This PR focuses on landing the smaller and immediately useful lint
alignment first, without mixing in the larger requirements-management
migration.
### Does this PR introduce _any_ user-facing change?
No.
This PR only updates repository lint configuration, static checks, and
internal import/style enforcement. It does not change runtime behavior
or public interfaces.
### How was this patch tested?
Tested locally in the project virtual environment.
Commands used:
```bash
bash format.sh
```
Verified checks passed:
``` bash
ruff check...............................................................Passed
ruff format..............................................................Passed
codespell................................................................Passed
typos....................................................................Passed
clang-format.............................................................Passed
Lint GitHub Actions workflow files.......................................Passed
Lint shell scripts.......................................................Passed
Lint PNG exports from excalidraw.........................................Passed
Check for spaces in all filenames........................................Passed
Enforce __init__.py in Python packages...................................Passed
Check for forbidden imports..............................................Passed
Check for boolean ops in with-statements.................................Passed
Suggestion...............................................................Passed
- hook id: suggestion
- duration: 0s
To bypass pre-commit hooks, add --no-verify to git commit.
```
**note:**
clang-format is enabled but currently excludes all csrc/**
- vLLM version: v0.17.0
- vLLM main:
8b6325758c
---------
Signed-off-by: MrZ20 <2609716663@qq.com>
88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
#
|
|
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
|
|
# Copyright 2023 The vLLM team.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
# This file is a part of the vllm-ascend project.
|
|
# Adapted from https://github.com/vllm-project/vllm/tree/main/tools
|
|
#
|
|
"""Lint: detect `with a() and b():` (boolean op in with-statement context).
|
|
|
|
Using `and`/`or` to combine context managers is almost always a bug:
|
|
|
|
with ctx_a() and ctx_b(): # BUG: only ctx_b is entered
|
|
with ctx_a() or ctx_b(): # BUG: only ctx_a is entered
|
|
|
|
The correct way to combine context managers is:
|
|
|
|
with ctx_a(), ctx_b(): # comma-separated
|
|
with (ctx_a(), ctx_b()): # parenthesized (Python 3.10+)
|
|
with contextlib.ExitStack() ... # ExitStack
|
|
"""
|
|
|
|
import ast
|
|
import sys
|
|
|
|
|
|
def check_file(filepath: str) -> list[str]:
|
|
try:
|
|
with open(filepath, encoding="utf-8") as f:
|
|
source = f.read()
|
|
except (OSError, UnicodeDecodeError):
|
|
return []
|
|
|
|
try:
|
|
tree = ast.parse(source, filename=filepath)
|
|
except SyntaxError:
|
|
return []
|
|
|
|
violations = []
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, (ast.With, ast.AsyncWith)):
|
|
for item in node.items:
|
|
if isinstance(item.context_expr, ast.BoolOp):
|
|
op = "and" if isinstance(item.context_expr.op, ast.And) else "or"
|
|
violations.append(
|
|
f"{filepath}:{item.context_expr.lineno}: "
|
|
f"boolean `{op}` used to combine context managers "
|
|
"in `with` statement; use a comma instead"
|
|
)
|
|
return violations
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) < 2:
|
|
print("Usage: check_boolean_context_manager.py <file> ...", file=sys.stderr)
|
|
return 1
|
|
|
|
all_violations = []
|
|
for filepath in sys.argv[1:]:
|
|
all_violations.extend(check_file(filepath))
|
|
|
|
if all_violations:
|
|
print(
|
|
"Boolean operator used to combine context managers in a `with` "
|
|
"statement.\n"
|
|
"Use `with a(), b():` or `with (a(), b()):` instead.\n"
|
|
)
|
|
for violation in all_violations:
|
|
print(f" {violation}")
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|