Rejig¶
Comprehensive Python code refactoring, analysis, and transformation.
Rejig provides a fluent API for finding and modifying Python code elements. Whether you're building codemods, automating refactoring, analyzing code quality, or managing large-scale migrations, Rejig gives you precise control over your codebase.
Features¶
Code Manipulation¶
- Target-based API — Everything is a target: files, modules, classes, functions, methods, lines
- Fluent chaining —
rj.file("app.py").find_class("User").find_method("save") - Batch operations — Apply changes to multiple targets with
TargetList - Atomic transactions — Collect changes and apply atomically with rollback
- Patch support — Parse, generate, apply, and reverse unified/git diffs
- Safe by default — Operations return
Resultobjects, never raise exceptions - Dry-run mode — Preview all changes before applying
Code Analysis¶
- Complexity analysis — Cyclomatic complexity, nesting depth, function length
- Dead code detection — Unused functions, classes, variables, imports
- Pattern detection — Missing type hints, bare excepts, magic numbers
- Security scanning — Hardcoded secrets, injection vulnerabilities, unsafe operations
- Optimization detection — Duplicate code, loop improvements
Code Transformation¶
- Import management — Organize, detect unused, fix circular imports
- Type hints — Infer, modernize, and generate type annotations
- Docstrings — Generate and update in Google, NumPy, or Sphinx style
- Code generation — Dunder methods, properties, test stubs
- Modernization — f-strings, Python 3.10+ syntax, deprecated API replacement
Project Management¶
- pyproject.toml — Manage metadata, dependencies, scripts
- Tool configuration — Black, Ruff, mypy, pytest, isort, coverage
- Package format conversion — requirements.txt, Poetry, PEP 621, UV
Framework Support¶
- Django — Settings, URLs, app discovery
- Flask — Routes, blueprints, error handlers
- FastAPI — Endpoints, dependencies, middleware
- SQLAlchemy — Models, relationships
Quick Example¶
from rejig import Rejig
rj = Rejig("src/")
# Find all test classes and add a decorator
rj.find_classes(pattern="^Test").add_decorator("pytest.mark.slow")
# Rename a method and update all references
rj.file("models.py").find_class("User").find_method("get_name").rename("get_full_name")
# Add type hints to all functions
rj.find_functions().infer_type_hints()
# Generate docstrings for functions without them
rj.find_functions().without_docstrings().generate_docstrings(style="google")
# Find security issues
security_issues = rj.find_security_issues()
print(f"Found {len(security_issues)} security issues")
# Modify pyproject.toml
rj.toml("pyproject.toml").set("tool.black.line-length", 110)
Why Rejig?¶
| Task | Without Rejig | With Rejig |
|---|---|---|
| Rename a method | Find/replace (breaks things) | target.rename("new_name") |
| Add decorator to many classes | Manual editing | targets.add_decorator("@cached") |
| Find security vulnerabilities | External tools, CI setup | rj.find_security_issues() |
| Add type hints to legacy code | Manual annotation | rj.find_functions().infer_type_hints() |
| Generate docstrings | Write each one | targets.generate_docstrings() |
| Update pyproject.toml | Parse, modify, serialize | rj.toml(path).set(key, value) |
| Find duplicate code | Manual review | DRYAnalyzer(rj).find_all_issues() |
Installation¶
Documentation¶
Getting Started¶
- Installation — Install and verify
- Quickstart — Get up and running in 5 minutes
- Core Concepts — Understand targets, results, and the API design
Guides¶
Finding & Modifying Code¶
- Finding Code — Locate classes, functions, methods, and more
- Modifying Code — Rename, add, remove, and transform code
- Batch Operations — Work with multiple targets
- Line Operations — Work with individual lines and ranges
Code Quality¶
- Code Analysis — Complexity, dead code, patterns
- Security Analysis — Find vulnerabilities and secrets
- Code Optimization — Detect duplicates and improvements
Code Transformation¶
- Import Management — Organize and manage imports
- Type Hints — Infer, modernize, and add type annotations
- Docstrings — Generate and update documentation
- Code Generation — Generate boilerplate code
Configuration¶
- Config Files — TOML, YAML, JSON, INI manipulation
- Project Management — pyproject.toml and dependencies
- Transactions — Atomic operations with rollback
- Patching — Parse, generate, apply, and reverse patches
Framework-Specific¶
- Django — Django project operations
- Flask — Flask application refactoring
- FastAPI — FastAPI endpoint management
Error Handling¶
- Error Handling — Result-based error handling patterns
Examples & Recipes¶
- Refactoring Patterns — Common refactoring scenarios
- Codemod Recipes — Ready-to-use migration scripts
- Optimization Recipes — Code quality improvements
- Analysis Recipes — Code analysis scripts
- Security Recipes — Security scanning scripts
API Reference¶
- Rejig Class — Main entry point
- Targets — All target classes
- Results — Result classes
- Patching — Patch parsing, generation, and targets
- Analysis — Analysis types and findings
- Security — Security types and findings
Sample Projects¶
- Sample Projects — Test projects for trying recipes