Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/scripts/check_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""检查所有Python文件是否可以成功编译和导入"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The docstring and filename check_imports.py are slightly misleading. The script uses py_compile, which primarily checks for syntax errors and can catch malformed import statements, but it doesn't perform a full import resolution that would detect runtime issues like circular dependencies or modules missing from sys.path. To be more accurate, the docstring should clarify that the script performs a syntax/compilation check.

Suggested change
"""检查所有Python文件是否可以成功编译和导入"""
"""检查所有Python文件的语法正确性"""


import os
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The os module is imported but is not used within the script. It's best practice to remove unused imports to maintain code cleanliness and avoid potential confusion.

import py_compile
import sys
from pathlib import Path


def check_syntax(file_path):
"""检查Python文件语法"""
try:
py_compile.compile(file_path, doraise=True)
return True, None
except py_compile.PyCompileError as e:
return False, str(e)


def main():
"""主函数"""
project_root = Path(__file__).parent.parent.parent
dingo_path = project_root / "dingo"

if not dingo_path.exists():
print(f"❌ 找不到dingo目录: {dingo_path}")
sys.exit(1)

errors = []
checked = 0

print("🔍 检查所有Python文件的语法和导入...")
print("-" * 60)

for py_file in dingo_path.rglob("*.py"):
if "__pycache__" in str(py_file):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current method of checking for __pycache__ by converting the path to a string is not fully robust. It could lead to false positives if a file or directory in the path contains __pycache__ as part of its name (e.g., my__pycache__project). A more reliable way is to check the components of the path using pathlib.Path.parts.

Suggested change
if "__pycache__" in str(py_file):
if "__pycache__" in py_file.parts:

continue

checked += 1
success, error = check_syntax(str(py_file))

if success:
print(f"✓ {py_file.relative_to(project_root)}")
else:
error_msg = f"✗ {py_file.relative_to(project_root)}: {error}"
print(error_msg)
errors.append(error_msg)

print("-" * 60)
print(f"📊 检查了 {checked} 个文件")

if errors:
print(f"\n❌ 发现 {len(errors)} 个错误:")
for error in errors:
print(f" {error}")
sys.exit(1)
else:
print(f"✅ 所有文件检查通过!")
sys.exit(0)


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions .github/workflows/IntegrationTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
if [ -f requirements/runtime.txt ]; then pip install -r requirements/runtime.txt; fi
pip install -e .

- name: Check Python syntax and imports
run: |
python .github/scripts/check_imports.py

- name: Integration Test(local plaintext)
run: |
python -m dingo.run.cli --input .github/env/local_plaintext.json
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ jobs:
- name: Install pre-commit
run: pip install pre-commit==3.8.0

- name: Install package
run: |
python -m pip install --upgrade pip
if [ -f requirements/runtime.txt ]; then pip install -r requirements/runtime.txt; fi
pip install -e .

- name: Check Python syntax and imports
run: |
python .github/scripts/check_imports.py

- name: Run pre-commit (auto-fix)
id: pre_commit_auto_fix
run: |
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

from setuptools import find_packages, setup

with open("README.md", "r", encoding='utf-8') as fh:
Expand Down