-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[CI] enforce import regex instead of re #18665
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
vllm-bot
merged 2 commits into
vllm-project:main
from
aarnphm:chore/enfore-import-regex-instead-of-re
May 24, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| FORBIDDEN_PATTERNS = re.compile( | ||
| r'^\s*(?:import\s+re(?:$|\s|,)|from\s+re\s+import)') | ||
| ALLOWED_PATTERNS = [ | ||
| re.compile(r'^\s*import\s+regex\s+as\s+re\s*$'), | ||
| re.compile(r'^\s*import\s+regex\s*$'), | ||
| ] | ||
|
|
||
|
|
||
| def get_staged_python_files() -> list[str]: | ||
| try: | ||
| result = subprocess.run( | ||
| ['git', 'diff', '--cached', '--name-only', '--diff-filter=AM'], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True) | ||
| files = result.stdout.strip().split( | ||
| '\n') if result.stdout.strip() else [] | ||
| return [f for f in files if f.endswith('.py')] | ||
| except subprocess.CalledProcessError: | ||
| return [] | ||
|
|
||
|
|
||
| def is_forbidden_import(line: str) -> bool: | ||
| line = line.strip() | ||
| return FORBIDDEN_PATTERNS.match(line) and not any( | ||
| pattern.match(line) for pattern in ALLOWED_PATTERNS) | ||
|
|
||
|
|
||
| def check_file(filepath: str) -> list[tuple[int, str]]: | ||
| violations = [] | ||
| try: | ||
| with open(filepath, encoding='utf-8') as f: | ||
| for line_num, line in enumerate(f, 1): | ||
| if is_forbidden_import(line): | ||
| violations.append((line_num, line.strip())) | ||
| except (OSError, UnicodeDecodeError): | ||
| pass | ||
| return violations | ||
|
|
||
|
|
||
| def main() -> int: | ||
| files = get_staged_python_files() | ||
| if not files: | ||
| return 0 | ||
|
|
||
| total_violations = 0 | ||
|
|
||
| for filepath in files: | ||
| if not Path(filepath).exists(): | ||
| continue | ||
|
|
||
| violations = check_file(filepath) | ||
| if violations: | ||
| print(f"\n❌ {filepath}:") | ||
| for line_num, line in violations: | ||
| print(f" Line {line_num}: {line}") | ||
| total_violations += 1 | ||
|
|
||
| if total_violations > 0: | ||
| print(f"\n💡 Found {total_violations} violation(s).") | ||
| print("❌ Please replace 'import re' with 'import regex as re'") | ||
| print( | ||
| " Also replace 'from re import ...' with 'from regex import ...'" | ||
| ) # noqa: E501 | ||
| print("✅ Allowed imports:") | ||
| print(" - import regex as re") | ||
| print(" - import regex") # noqa: E501 | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.