Skip to content

Commit 7b5a062

Browse files
committed
add python init check
Signed-off-by: wangli <wangli858794774@gmail.com>
1 parent 4907eb8 commit 7b5a062

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ repos:
130130
types: [python]
131131
pass_filenames: false
132132
additional_dependencies: [regex]
133+
- id: python-init
134+
name: Enforce __init__.py in Python packages
135+
entry: python tools/check_python_pkg_init.py
136+
language: python
137+
types: [python]
138+
pass_filenames: false
133139
# Keep `suggestion` last
134140
- id: suggestion
135141
name: Suggestion

tools/check_python_pkg_init.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
3+
# Copyright 2023 The vLLM team.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# This file is a part of the vllm-ascend project.
17+
# Adapted from https://github.com/vllm-project/vllm/tree/main/tools
18+
#
19+
import os
20+
import sys
21+
22+
VLLM_ASCEND_SRC = "vllm_ascend"
23+
VLLM_SRC = "vllm-empty/vllm"
24+
25+
26+
def check_init_file_in_package(directory):
27+
"""
28+
Check if a Python package directory contains __init__.py file.
29+
A directory is considered a Python package if it contains `.py` files and an `__init__.py` file.
30+
"""
31+
try:
32+
files = os.listdir(directory)
33+
except FileNotFoundError:
34+
print(f"Warning: Directory does not exist: {directory}")
35+
return False
36+
37+
# If any .py file exists, we expect an __init__.py
38+
if any(f.endswith('.py') for f in files):
39+
init_file = os.path.join(directory, '__init__.py')
40+
if not os.path.isfile(init_file):
41+
return False
42+
return True
43+
44+
45+
def find_missing_init_dirs(src_dir):
46+
"""
47+
Walk through the src_dir and return subdirectories missing __init__.py.
48+
"""
49+
missing_init = set()
50+
for dirpath, _, _ in os.walk(src_dir):
51+
if not check_init_file_in_package(dirpath):
52+
missing_init.add(dirpath)
53+
return missing_init
54+
55+
56+
def main():
57+
all_missing = set()
58+
59+
for src in [VLLM_ASCEND_SRC, VLLM_SRC]:
60+
missing = find_missing_init_dirs(src)
61+
all_missing.update(missing)
62+
63+
if all_missing:
64+
print(
65+
"❌ Missing '__init__.py' files in the following Python package directories:"
66+
)
67+
for pkg in sorted(all_missing):
68+
print(f" - {pkg}")
69+
sys.exit(1)
70+
else:
71+
print("✅ All Python packages have __init__.py files.")
72+
73+
74+
if __name__ == "__main__":
75+
main()

tools/shellcheck.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
set -e
2323

24-
scversion="v0.10.0"
24+
scversion="stable"
2525

2626
if [ -d "shellcheck-${scversion}" ]; then
2727
PATH="$PATH:$(pwd)/shellcheck-${scversion}"

0 commit comments

Comments
 (0)