A comprehensive Python refactoring library providing powerful and safe code transformations, analysis, and IDE integration capabilities.
npx @tessl/cli install tessl/pypi-rope@1.14.0Rope is the world's most advanced open source Python refactoring library, providing comprehensive code analysis and transformation capabilities. It supports most Python syntax up to Python 3.10 and offers powerful refactoring operations while maintaining code safety through careful static analysis.
pip install ropeimport rope.base.project
from rope.base.project import ProjectFor refactoring operations:
from rope.refactor.rename import Rename
from rope.refactor.move import MoveMethod, MoveModule
from rope.refactor.extract import ExtractMethod, ExtractVariableFor code assistance:
from rope.contrib.codeassist import code_assist, get_definition_location
from rope.contrib.findit import find_occurrencesfrom rope.base.project import Project
from rope.refactor.rename import Rename
# Create or open a project
project = Project('/path/to/your/project')
try:
# Get a Python file
mymodule = project.get_resource('mymodule.py')
# Perform a rename refactoring
# Rename variable at offset 150 to 'new_name'
renamer = Rename(project, mymodule, 150)
changes = renamer.get_changes('new_name')
# Apply the changes
project.do(changes)
finally:
# Always close the project
project.close()Rope follows a layered architecture designed for safety and extensibility:
The design emphasizes safety through static analysis, avoiding runtime execution of analyzed code, making it suitable for IDE integration and automated refactoring tools.
Core functionality for managing Python projects, resources, and configuration. Provides the foundation for all rope operations with file system abstraction and project state management.
class Project:
def __init__(self, root_folder, **prefs): ...
def get_resource(self, resource_name): ...
def get_module(self, name, folder=None): ...
def close(self): ...
def get_no_project(): ...Comprehensive refactoring capabilities including rename, move, extract, inline, and restructure operations. All refactorings follow the standard pattern of construction, information gathering, change generation, and execution.
class Rename:
def __init__(self, project, resource, offset): ...
def get_old_name(self): ...
def get_changes(self, new_name, **kwargs): ...
class ExtractMethod:
def __init__(self, project, resource, start_offset, end_offset): ...
def get_changes(self, method_name, **kwargs): ...IDE integration tools providing code completion, documentation lookup, definition finding, and occurrence detection. Designed for real-time assistance in development environments.
def code_assist(project, source_code, offset, resource=None, **kwargs): ...
def get_definition_location(project, source_code, offset, **kwargs): ...
def find_occurrences(project, resource, offset, **kwargs): ...System for representing, combining, and applying code changes safely. Enables previewing modifications before applying them and supports undo operations through project history.
class Change:
def do(self): ...
def get_description(self): ...
class ChangeSet:
def add_change(self, change): ...
def do(self): ...Exception hierarchy for handling various error conditions during code analysis and refactoring operations.
class RopeError(Exception): ...
class RefactoringError(RopeError): ...
class ResourceNotFoundError(RopeError): ...
class ModuleNotFoundError(RopeError): ...class Resource:
"""Base class for project resources (files and folders)."""
path: str
name: str
parent: Resource
project: Project
def move(self, new_location): ...
def remove(self): ...
def exists(self): ...
class File(Resource):
"""File resource with content operations."""
def read(self): ...
def write(self, contents): ...
class Folder(Resource):
"""Folder resource with child management."""
def get_children(self): ...
def get_child(self, name): ...
def create_file(self, name): ...
def create_folder(self, name): ...