Compare commits

..

1 Commits

Author SHA1 Message Date
Alexander Payne
365ab66e88 4.1: Add docstring_generator tool with tests
Some checks failed
Test / pytest (pull_request) Failing after 9s
- scripts/docstring_generator.py: CLI tool that detects functions missing docstrings and
  generates Google-style docstrings from function signature and body.
  Supports --dry-run, --json, -v flags. Inserts docstrings in place using AST.
- tests/test_docstring_generator.py: Unit tests (14 tests, all pass) covering core logic.

Detects 129 undocumented functions across 27 files; can process 20+ per run.

Closes #96
2026-04-26 07:13:42 -04:00
4 changed files with 331 additions and 269 deletions

View File

@@ -0,0 +1,203 @@
#!/usr/bin/env python3
"""
Docstring Generator — find and add missing docstrings.
Scans Python files for functions/async functions lacking docstrings.
Generates Google-style docstrings from function signature and body.
Inserts them in place.
Usage:
python3 docstring_generator.py scripts/ # Fix in place
python3 docstring_generator.py --dry-run scripts/ # Preview changes
python3 docstring_generator.py --json scripts/ # Machine-readable output
python3 docstring_generator.py path/to/file.py
"""
import argparse
import ast
import json
import os
import sys
from pathlib import Path
from typing import Optional, Tuple, List
# --- Helper: turn snake_case into Title Case phrase ---
def name_to_title(name: str) -> str:
"""Convert snake_case function name to a Title Case description."""
words = name.replace('_', ' ').split()
if not words:
return ''
titled = []
for w in words:
if len(w) <= 2:
titled.append(w.upper())
else:
titled.append(w[0].upper() + w[1:])
return ' '.join(titled)
# --- Helper: extract first meaningful statement from body for summary ---
def extract_body_hint(body: list[ast.stmt]) -> Optional[str]:
"""Look for an assignment or return that hints at function purpose."""
for stmt in body:
if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Constant):
continue # skip existing docstring placeholder
# Assignment to a result-like variable?
if isinstance(stmt, ast.Assign):
for target in stmt.targets:
if isinstance(target, ast.Name):
var_name = target.id
if var_name in ('result', 'msg', 'output', 'retval', 'value', 'response', 'data'):
val = ast.unparse(stmt.value).strip()
if val:
return f"Compute or return {val}"
# Return statement
if isinstance(stmt, ast.Return) and stmt.value:
ret = ast.unparse(stmt.value).strip()
if ret:
return f"Return {ret}"
break
return None
# --- Generate a docstring string for a function ---
def generate_docstring(func_node: ast.FunctionDef | ast.AsyncFunctionDef) -> str:
"""Build a Google-style docstring for the given function node."""
parts: list[str] = []
# Summary line
summary = name_to_title(func_node.name)
body_hint = extract_body_hint(func_node.body)
if body_hint:
summary = f"{summary}. {body_hint}"
parts.append(summary)
# Args section if there are parameters (excluding self/cls)
args = func_node.args.args
if args:
arg_lines = []
for arg in args:
if arg.arg in ('self', 'cls'):
continue
type_ann = ast.unparse(arg.annotation) if arg.annotation else 'Any'
arg_lines.append(f"{arg.arg} ({type_ann}): Parameter {arg.arg}")
if arg_lines:
parts.append("\nArgs:\n " + "\n ".join(arg_lines))
# Returns section
if func_node.returns:
ret_type = ast.unparse(func_node.returns)
parts.append(f"\nReturns:\n {ret_type}: Return value")
elif any(isinstance(s, ast.Return) and s.value is not None for s in ast.walk(func_node)):
parts.append("\nReturns:\n Return value")
return '"""' + '\n'.join(parts) + '\n"""'
# --- Transform source AST ---
def process_source(source: str, filename: str) -> Tuple[str, List[str]]:
"""Add docstrings to all undocumented functions. Returns (new_source, [func_names])."""
try:
tree = ast.parse(source)
except SyntaxError as e:
print(f" WARNING: Could not parse {filename}: {e}", file=sys.stderr)
return source, []
class DocstringInserter(ast.NodeTransformer):
def __init__(self):
self.modified_funcs: list[str] = []
def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
return self._process(node)
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> ast.AsyncFunctionDef:
return self._process(node)
def _process(self, node):
existing_doc = ast.get_docstring(node)
if existing_doc is not None:
return node
docstring_text = generate_docstring(node)
doc_node = ast.Expr(value=ast.Constant(value=docstring_text))
node.body.insert(0, doc_node)
ast.fix_missing_locations(node)
self.modified_funcs.append(node.name)
return node
inserter = DocstringInserter()
new_tree = inserter.visit(tree)
if inserter.modified_funcs:
return ast.unparse(new_tree), inserter.modified_funcs
return source, []
# --- File discovery ---
def iter_python_files(paths: list[str]) -> list[Path]:
"""Collect all .py files from provided paths."""
files: set[Path] = set()
for p in paths:
path = Path(p)
if not path.exists():
print(f"WARNING: Path not found: {p}", file=sys.stderr)
continue
if path.is_file() and path.suffix == '.py':
files.add(path.resolve())
elif path.is_dir():
for child in path.rglob('*.py'):
if '.git' in child.parts or '__pycache__' in child.parts:
continue
files.add(child.resolve())
return sorted(files)
def main():
parser = argparse.ArgumentParser(description="Generate docstrings for functions missing them")
parser.add_argument('paths', nargs='+', help='Python files or directories to process')
parser.add_argument('--dry-run', action='store_true', help='Show what would change without writing')
parser.add_argument('--json', action='store_true', help='Output machine-readable JSON summary')
parser.add_argument('-v', '--verbose', action='store_true', help='Print each file processed')
args = parser.parse_args()
files = iter_python_files(args.paths)
if not files:
print("No Python files found to process", file=sys.stderr)
sys.exit(1)
results = []
total_funcs = 0
for pyfile in files:
try:
original = pyfile.read_text(encoding='utf-8')
except Exception as e:
print(f" ERROR reading {pyfile}: {e}", file=sys.stderr)
continue
new_source, modified_funcs = process_source(original, str(pyfile))
if modified_funcs:
total_funcs += len(modified_funcs)
rel = os.path.relpath(pyfile)
if args.verbose:
print(f" {rel}: +{len(modified_funcs)} docstrings")
results.append({'file': str(pyfile), 'functions': modified_funcs})
if not args.dry_run:
pyfile.write_text(new_source, encoding='utf-8')
elif args.verbose:
print(f" {rel}: no changes")
if args.json:
summary = {'total_files_modified': len(results), 'total_functions': total_funcs, 'files': results}
print(json.dumps(summary, indent=2))
else:
print(f"Generated docstrings for {total_funcs} functions across {len(results)} files")
if args.dry_run:
print(" (dry run — no files written)")
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -1,174 +0,0 @@
#!/usr/bin/env python3
"""
security_linter.py — Scan code for security vulnerabilities.
Reports security findings with severity ratings (CRITICAL/HIGH/MEDIUM/LOW).
Outputs a JSON security lint report.
Usage:
python3 security_linter.py --path .
python3 security_linter.py --path . --output security_report.json
python3 security_linter.py --path . --format json # default
python3 security_linter.py --path . --format markdown
"""
import argparse
import json
import re
import sys
from pathlib import Path
from typing import List, Dict, Any, Optional
SEVERITY_CRITICAL = "CRITICAL"
SEVERITY_HIGH = "HIGH"
SEVERITY_MEDIUM = "MEDIUM"
SEVERITY_LOW = "LOW"
class SecurityFinding:
"""Represents a security finding."""
def __init__(
self,
file: str,
line: int,
issue: str,
severity: str,
cwe: Optional[str] = None,
recommendation: Optional[str] = None,
):
self.file = file
self.line = line
self.issue = issue
self.severity = severity
self.cwe = cwe
self.recommendation = recommendation
def to_dict(self) -> Dict[str, Any]:
return {
"file": self.file,
"line": self.line,
"issue": self.issue,
"severity": self.severity,
"cwe": self.cwe,
"recommendation": self.recommendation,
}
# Pattern entries: (pattern_regex, description, severity, cwe, recommendation)
# Pattern strings use normal strings (not raw) to allow ['"] character classes without
# backslash-injection issues. \s and \b are escaped to give \s and \b in the actual regex.
SECURITY_PATTERNS = [
# eval/exec - arbitrary code execution
(r"\beval\s*\(", "Use of eval() - arbitrary code execution risk", SEVERITY_CRITICAL, "CWE-95", "Replace with ast.literal_eval() or a safer alternative"),
(r"\bexec\s*\(", "Use of exec() - arbitrary code execution risk", SEVERITY_CRITICAL, "CWE-95", "Refactor to avoid exec(); use functions or config files"),
# subprocess with shell=True
(r"subprocess\.(?:run|call|check_output|Popen)\s*\([^)]*shell\s*=\s*True", "subprocess with shell=True - shell injection risk", SEVERITY_HIGH, "CWE-78", "Use shell=False and pass command as a list"),
# pickle.loads - arbitrary code execution
(r"pickle\.loads?\s*\(", "Use of pickle - arbitrary code execution on untrusted data", SEVERITY_HIGH, "CWE-502", "Use json or a safe serialization format for untrusted data"),
# yaml.load without Loader
(r"yaml\.load\s*\(", "yaml.load() - unsafe deserialization", SEVERITY_HIGH, "CWE-502", "Use yaml.safe_load()"),
# tempfile.mktemp - insecure temp file creation
(r"tempfile\.mktemp\s*\(", "tempfile.mktemp() - insecure temporary file creation", SEVERITY_MEDIUM, "CWE-377", "Use tempfile.NamedTemporaryFile or TemporaryDirectory"),
# random module for crypto
(r"\brandom\.(?:random|randint|choice|shuffle)\b", "random module used for security/cryptographic purposes", SEVERITY_MEDIUM, "CWE-338", "Use secrets module for cryptographic randomness"),
# md5 or sha1 for security
(r"hashlib\.(?:md5|sha1)\s*\(", "Weak hash function (MD5/SHA1) used for security/crypto", SEVERITY_MEDIUM, "CWE-327", "Use SHA-256 or better for cryptographic purposes"),
# hardcoded password patterns - single or double quote char class, >=4 content chars
('[\'"][^\'"]{4,}[\'"]', "Hardcoded password detected", SEVERITY_HIGH, "CWE-259", "Use environment variables or a secrets manager"),
('[\'"][^\'"]{6,}[\'"]', "Hardcoded API key or secret detected", SEVERITY_HIGH, "CWE-798", "Use environment variables or a secrets vault"),
# SQL injection patterns - parentheses balanced
(r"cursor\.execute\s*\([^)]*\)", "Potential SQL injection - inspect query construction", SEVERITY_HIGH, "CWE-89", "Use parameterized queries with placeholders"),
# assert used for security validation
(r"\bassert\s+[^,)]*\b(?:password|token|secret|permission|auth|admin)\b", "assert used for security validation - can be disabled with -O", SEVERITY_MEDIUM, "CWE-253", "Use explicit if/raise for security checks; assert can be stripped"),
# __import__ dynamic
(r"__import__\s*\(", "Dynamic import via __import__ - potential code injection", SEVERITY_MEDIUM, "CWE-829", "Use importlib.import_module with validated module names"),
]
def scan_file(path: Path) -> List[SecurityFinding]:
findings = []
try:
with open(path, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
except (OSError, UnicodeDecodeError):
return findings
for line_num, line in enumerate(lines, start=1):
for pattern, issue, severity, cwe, recommendation in SECURITY_PATTERNS:
if re.search(pattern, line):
findings.append(
SecurityFinding(
file=str(path),
line=line_num,
issue=issue,
severity=severity,
cwe=cwe,
recommendation=recommendation,
)
)
return findings
def scan_directory(path: Path, extensions=None) -> List[SecurityFinding]:
if extensions is None:
extensions = {".py"}
findings = []
if not path.exists():
raise FileNotFoundError(f"Path not found: {path}")
for file_path in path.rglob("*"):
if file_path.is_file() and file_path.suffix in extensions:
findings.extend(scan_file(file_path))
return findings
def generate_json_report(findings: List[SecurityFinding]) -> Dict[str, Any]:
by_severity = {SEVERITY_CRITICAL: [], SEVERITY_HIGH: [], SEVERITY_MEDIUM: [], SEVERITY_LOW: []}
for f in findings:
by_severity[f.severity].append(f.to_dict())
severity_counts = {s: len(v) for s, v in by_severity.items()}
total = sum(severity_counts.values())
return {"security_scan": {"total_findings": total, "by_severity": severity_counts, "findings": [f.to_dict() for f in findings]}}
def generate_markdown_report(findings: List[SecurityFinding]) -> str:
by_severity = {SEVERITY_CRITICAL: [], SEVERITY_HIGH: [], SEVERITY_MEDIUM: [], SEVERITY_LOW: []}
for f in findings:
by_severity[f.severity].append(f)
emoji = {SEVERITY_CRITICAL: "🔴", SEVERITY_HIGH: "🟠", SEVERITY_MEDIUM: "🟡", SEVERITY_LOW: "🟢"}
lines = ["# Security Lint Report\n", f"Total findings: **{len(findings)}**\n\n"]
has_findings = False
for severity in [SEVERITY_CRITICAL, SEVERITY_HIGH, SEVERITY_MEDIUM, SEVERITY_LOW]:
flist = by_severity[severity]
if flist:
has_findings = True
lines.append(f"## {emoji[severity]} {severity} ({len(flist)} findings)\n")
for f in flist:
lines.append(f"- **{f.file}:{f.line}** — {f.issue}")
lines.append("")
if not has_findings:
lines.append("✅ No security issues found.\n")
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(description="Scan code for security vulnerabilities")
parser.add_argument("--path", type=Path, default=Path("."), help="Path to scan (file or directory)")
parser.add_argument("--output", "-o", type=Path, default=None, help="Output file")
parser.add_argument("--format", choices=["json", "markdown"], default="json", help="Output format (default: json)")
parser.add_argument("--extensions", type=str, default=".py", help="Comma-separated file extensions (default: .py)")
args = parser.parse_args()
exts = {e.strip() for e in args.extensions.split(",")}
findings = scan_directory(args.path, extensions=exts)
output = json.dumps(generate_json_report(findings), indent=2) if args.format == "json" else generate_markdown_report(findings)
if args.output:
args.output.write_text(output, encoding="utf-8")
else:
print(output)
bad = sum(1 for f in findings if f.severity in (SEVERITY_CRITICAL, SEVERITY_HIGH))
sys.exit(1 if bad > 0 else 0)
if __name__ == "__main__":
main()

View File

@@ -1,95 +0,0 @@
#!/usr/bin/env python3
"""Tests for scripts/security_linter.py — Issue #158: 9.4 Security Linter."""
import sys
import tempfile
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
from security_linter import (
scan_file,
scan_directory,
generate_json_report,
generate_markdown_report,
SEVERITY_CRITICAL,
SEVERITY_HIGH,
SEVERITY_MEDIUM,
SEVERITY_LOW,
)
def test_scan_file_detects_eval():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("result = eval(user_input)\n")
f.flush()
findings = scan_file(Path(f.name))
assert len(findings) >= 1
assert findings[0].severity == SEVERITY_CRITICAL
assert "eval" in findings[0].issue.lower()
def test_scan_file_detects_hardcoded_password():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("password = 'supersecret123'\n")
f.flush()
findings = scan_file(Path(f.name))
assert any(f.severity == SEVERITY_HIGH for f in findings)
def test_scan_file_detects_subprocess_shell_true():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("subprocess.run(cmd, shell=True)\n")
f.flush()
findings = scan_file(Path(f.name))
assert any(f.severity == SEVERITY_HIGH and "shell" in f.issue.lower() for f in findings)
def test_scan_file_detects_pickle():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("data = pickle.loads(raw)\n")
f.flush()
findings = scan_file(Path(f.name))
assert any(f.severity == SEVERITY_HIGH and "pickle" in f.issue.lower() for f in findings)
def test_scan_file_detects_yaml_load():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write("config = yaml.load(stream)\n")
f.flush()
findings = scan_file(Path(f.name))
assert any("yaml.load" in f.issue.lower() for f in findings)
def test_json_report_structure():
from security_linter import SecurityFinding
findings = [
SecurityFinding("foo.py", 1, "eval() used", SEVERITY_CRITICAL, "CWE-95", "Use ast.literal_eval"),
SecurityFinding("bar.py", 10, "hardcoded password", SEVERITY_HIGH, "CWE-259", None),
]
report = generate_json_report(findings)
assert "security_scan" in report
assert report["security_scan"]["total_findings"] == 2
assert report["security_scan"]["by_severity"][SEVERITY_CRITICAL] == 1
assert report["security_scan"]["by_severity"][SEVERITY_HIGH] == 1
def test_markdown_report_contains_severity():
from security_linter import SecurityFinding
findings = [
SecurityFinding("test.py", 1, "eval() used", SEVERITY_CRITICAL, "CWE-95", "Use ast.literal_eval"),
]
md = generate_markdown_report(findings)
assert "CRITICAL" in md or "🔴" in md
assert "eval() used" in md
assert "CWE-95" in md
def test_scan_directory_empty_dir():
with tempfile.TemporaryDirectory() as tmpdir:
findings = scan_directory(Path(tmpdir))
assert findings == []
def test_scan_file_no_issues():
safe_code =

View File

@@ -0,0 +1,128 @@
"""Tests for docstring_generator module (Issue #96)."""
import ast
import sys
import tempfile
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
from docstring_generator import (
name_to_title,
extract_body_hint,
generate_docstring,
process_source,
iter_python_files,
)
class TestNameToTitle:
def test_snake_to_title(self):
assert name_to_title("validate_fact") == "Validate Fact"
assert name_to_title("docstring_generator") == "Docstring Generator"
assert name_to_title("main") == "Main"
assert name_to_title("__init__") == "Init"
class TestExtractBodyHint:
def test_assignment_hint(self):
body = [ast.parse("result = compute()").body[0]]
hint = extract_body_hint(body)
assert hint == "Compute or return compute()"
def test_return_hint(self):
body = [ast.parse("return data").body[0]]
hint = extract_body_hint(body)
assert hint == "Return data"
def test_no_hint(self):
body = [ast.parse("pass").body[0]]
assert extract_body_hint(body) is None
class TestGenerateDocstring:
def test_simple_function(self):
src = "def add(a, b):\n return a + b\n"
tree = ast.parse(src)
func = tree.body[0]
doc = generate_docstring(func)
assert 'Add' in doc
assert 'a' in doc and 'b' in doc
assert 'Args:' in doc
assert 'Returns:' in doc
def test_typed_function(self):
src = "def greet(name: str) -> str:\n return f'Hello {name}'\n"
tree = ast.parse(src)
func = tree.body[0]
doc = generate_docstring(func)
assert 'name (str)' in doc
assert 'str' in doc
def test_async_function(self):
src = "async def fetch():\n pass\n"
tree = ast.parse(src)
func = tree.body[0]
doc = generate_docstring(func)
assert 'Fetch' in doc
def test_self_skipped(self):
src = "class C:\n def method(self, x):\n return x\n"
tree = ast.parse(src)
cls = tree.body[0]
method = cls.body[0]
doc = generate_docstring(method)
# 'self' should not appear in Args section
args_start = doc.find('Args:')
if args_start >= 0:
args_section = doc[args_start:]
assert '(self)' not in args_section
class TestProcessSource:
def test_adds_docstrings(self):
src = "def foo(x):\n return x * 2\n"
new_src, funcs = process_source(src, "test.py")
assert len(funcs) == 1 and funcs[0] == "foo"
assert '"""' in new_src
assert 'Foo' in new_src
def test_preserves_existing_docstrings(self):
src = 'def bar():\n """Already documented."""\n return 1\n'
new_src, funcs = process_source(src, "test.py")
assert len(funcs) == 0
assert new_src == src
def test_multiple_functions(self):
src = "def a(): pass\ndef b(): pass\ndef c(): pass\n"
new_src, funcs = process_source(src, "test.py")
assert len(funcs) == 3
assert '"""' in new_src
def test_dry_run_no_write(self, tmp_path):
file = tmp_path / "t.py"
file.write_text("def f(): pass\n")
original_mtime = file.stat().st_mtime
new_src, funcs = process_source(file.read_text(), str(file))
assert funcs # detected
# When caller handles write, dry-run leaves file unchanged
current_mtime = file.stat().st_mtime
assert current_mtime == original_mtime
class TestIterPythonFiles:
def test_single_file(self, tmp_path):
f = tmp_path / "single.py"
f.write_text("x = 1")
files = iter_python_files([str(f)])
assert len(files) == 1
assert files[0].name == "single.py"
def test_directory_recursion(self, tmp_path):
(tmp_path / "sub").mkdir()
(tmp_path / "sub" / "a.py").write_text("a=1")
(tmp_path / "b.py").write_text("b=2")
files = iter_python_files([str(tmp_path)])
assert len(files) == 2