fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
This commit is contained in:
AutoFix Bot
2026-03-03 06:03:38 +08:00
parent 2a0ed6af4d
commit 9fd1da8fb7
41 changed files with 901 additions and 768 deletions

View File

@@ -55,7 +55,7 @@ class CodeFixer:
def _scan_file(self, file_path: Path) -> None:
"""扫描单个文件"""
try:
with open(file_path, "r", encoding="utf-8") as f:
with open(file_path, encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")
except Exception as e:
@@ -81,7 +81,7 @@ class CodeFixer:
self._check_sensitive_info(file_path, content, lines)
def _check_bare_exceptions(
self, file_path: Path, content: str, lines: list[str]
self, file_path: Path, content: str, lines: list[str],
) -> None:
"""检查裸异常捕获"""
for i, line in enumerate(lines, 1):
@@ -98,11 +98,11 @@ class CodeFixer:
"裸异常捕获,应指定具体异常类型",
"error",
line,
)
),
)
def _check_pep8_issues(
self, file_path: Path, content: str, lines: list[str]
self, file_path: Path, content: str, lines: list[str],
) -> None:
"""检查 PEP8 格式问题"""
for i, line in enumerate(lines, 1):
@@ -116,7 +116,7 @@ class CodeFixer:
f"行长度 {len(line)} 超过 120 字符",
"warning",
line,
)
),
)
# 行尾空格(排除空行)
@@ -129,7 +129,7 @@ class CodeFixer:
"行尾有空格",
"info",
line,
)
),
)
def _check_unused_imports(self, file_path: Path, content: str) -> None:
@@ -171,11 +171,11 @@ class CodeFixer:
f"未使用的导入: {name}",
"warning",
"",
)
),
)
def _check_string_formatting(
self, file_path: Path, content: str, lines: list[str]
self, file_path: Path, content: str, lines: list[str],
) -> None:
"""检查字符串格式化"""
for i, line in enumerate(lines, 1):
@@ -193,18 +193,18 @@ class CodeFixer:
"使用 % 格式化,建议改为 f-string",
"info",
line,
)
),
)
def _check_cors_config(
self, file_path: Path, content: str, lines: list[str]
self, file_path: Path, content: str, lines: list[str],
) -> None:
"""检查 CORS 配置"""
for i, line in enumerate(lines, 1):
if "allow_origins" in line and '["*"]' in line:
# 排除扫描工具自身的代码
if "code_reviewer" in str(file_path) or "auto_code_fixer" in str(
file_path
file_path,
):
continue
self.manual_issues.append(
@@ -215,11 +215,11 @@ class CodeFixer:
"CORS 配置允许所有来源 (*),生产环境应限制具体域名",
"warning",
line,
)
),
)
def _check_sensitive_info(
self, file_path: Path, content: str, lines: list[str]
self, file_path: Path, content: str, lines: list[str],
) -> None:
"""检查敏感信息泄露"""
# 排除的文件
@@ -261,7 +261,7 @@ class CodeFixer:
f"{desc},应使用环境变量",
"critical",
line,
)
),
)
def fix_auto_fixable(self) -> None:
@@ -285,7 +285,7 @@ class CodeFixer:
continue
try:
with open(file_path, "r", encoding="utf-8") as f:
with open(file_path, encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")
except Exception:
@@ -314,7 +314,7 @@ class CodeFixer:
# 将 except Exception: 改为 except Exception:
if re.search(r"except\s*:\s*$", line.strip()):
lines[line_idx] = line.replace(
"except Exception:", "except Exception:"
"except Exception:", "except Exception:",
)
fixed_lines.add(line_idx)
issue.fixed = True
@@ -368,11 +368,11 @@ class CodeFixer:
report.append("## 问题分类统计")
report.append("")
report.append(
f"- 🔴 Critical: {len(categories['critical']) + len(manual_critical)}"
f"- 🔴 Critical: {len(categories['critical']) + len(manual_critical)}",
)
report.append(f"- 🟠 Error: {len(categories['error'])}")
report.append(
f"- 🟡 Warning: {len(categories['warning']) + len(manual_warning)}"
f"- 🟡 Warning: {len(categories['warning']) + len(manual_warning)}",
)
report.append(f"- 🔵 Info: {len(categories['info'])}")
report.append(f"- **总计: {len(self.issues) + len(self.manual_issues)}**")
@@ -384,7 +384,7 @@ class CodeFixer:
if self.fixed_issues:
for issue in self.fixed_issues:
report.append(
f"- `{issue.file_path}:{issue.line_no}` - {issue.issue_type}: {issue.message}"
f"- `{issue.file_path}:{issue.line_no}` - {issue.issue_type}: {issue.message}",
)
else:
report.append("")
@@ -396,7 +396,7 @@ class CodeFixer:
if self.manual_issues:
for issue in self.manual_issues:
report.append(
"- `{issue.file_path}:{issue.line_no}` [{issue.severity}] {issue.message}"
"- `{issue.file_path}:{issue.line_no}` [{issue.severity}] {issue.message}",
)
if issue.original_line:
report.append(" ```python")
@@ -423,7 +423,7 @@ class CodeFixer:
report.append("")
for issue in issues[:10]: # 每种类型最多显示10个
report.append(
f"- `{issue.file_path}:{issue.line_no}` - {issue.message}"
f"- `{issue.file_path}:{issue.line_no}` - {issue.message}",
)
if len(issues) > 10:
report.append(f"- ... 还有 {len(issues) - 10} 个类似问题")
@@ -458,7 +458,7 @@ def git_commit_and_push(project_path: str) -> tuple[bool, str]:
- 添加类型注解"""
subprocess.run(
["git", "commit", "-m", commit_msg], cwd=project_path, check=True
["git", "commit", "-m", commit_msg], cwd=project_path, check=True,
)
# 推送