Skip to content

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 chainingrj.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 Result objects, 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

pip install rejig

# With framework support
pip install rejig[django]
pip install rejig[all]

Documentation

Getting Started

Guides

Finding & Modifying Code

Code Quality

Code Transformation

Configuration

Framework-Specific

  • Django — Django project operations
  • Flask — Flask application refactoring
  • FastAPI — FastAPI endpoint management

Error Handling

Examples & Recipes

API Reference

Sample Projects