lint_check.py
lint_check.pyBrowse 2 files
538 tokens
2,273 bytes
Token encoding: o200k_base
Snapshot 229ffef
← Back to SKILL.md
1#!/usr/bin/env python32"""Quick lint check helper for the code-review skill.3 4Scans Python files for common issues that a full linter might miss or that5are worth flagging during code review:6 7- Files missing a module docstring8- Functions longer than 50 lines9- Bare `except:` clauses10 11Usage::12 13 python /skills/code-review/lint_check.py [path ...]14 15If no paths are given, scans the current directory recursively.16"""17 18import ast19import sys20from pathlib import Path21 22 23def check_file(path: Path) -> list[str]:24 """Return a list of warnings for a single Python file."""25 warnings: list[str] = []26 try:27 source = path.read_text(encoding="utf-8")28 except Exception as exc:29 return [f"{path}: could not read ({exc})"]30 31 try:32 tree = ast.parse(source, filename=str(path))33 except SyntaxError as exc:34 return [f"{path}:{exc.lineno}: syntax error: {exc.msg}"]35 36 # Check for missing module docstring37 if not ast.get_docstring(tree):38 warnings.append(f"{path}:1: missing module docstring")39 40 for node in ast.walk(tree):41 # Long functions42 if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):43 length = (node.end_lineno or node.lineno) - node.lineno + 144 if length > 50:45 warnings.append(46 f"{path}:{node.lineno}: function '{node.name}' is {length} lines long (>50)"47 )48 49 # Bare except50 if isinstance(node, ast.ExceptHandler) and node.type is None:51 warnings.append(f"{path}:{node.lineno}: bare 'except:' clause")52 53 return warnings54 55 56def main(paths: list[str]) -> int:57 targets = [Path(p) for p in paths] if paths else [Path(".")]58 all_warnings: list[str] = []59 60 for target in targets:61 if target.is_file() and target.suffix == ".py":62 all_warnings.extend(check_file(target))63 elif target.is_dir():64 for py_file in sorted(target.rglob("*.py")):65 all_warnings.extend(check_file(py_file))66 67 for w in all_warnings:68 print(w)69 70 if all_warnings:71 print(f"\n{len(all_warnings)} warning(s) found.")72 return 173 74 print("No warnings found.")75 return 076 77 78if __name__ == "__main__":79 sys.exit(main(sys.argv[1:]))80