or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

change-management.mdcode-assistance.mderror-handling.mdindex.mdproject-management.mdrefactoring-operations.md
tile.json

tessl/pypi-rope

A comprehensive Python refactoring library providing powerful and safe code transformations, analysis, and IDE integration capabilities.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/rope@1.14.x

To install, run

npx @tessl/cli install tessl/pypi-rope@1.14.0

index.mddocs/

Rope

Rope 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.

Package Information

  • Package Name: rope
  • Language: Python
  • Installation: pip install rope
  • License: LGPL-3.0-or-later

Core Imports

import rope.base.project
from rope.base.project import Project

For refactoring operations:

from rope.refactor.rename import Rename
from rope.refactor.move import MoveMethod, MoveModule
from rope.refactor.extract import ExtractMethod, ExtractVariable

For code assistance:

from rope.contrib.codeassist import code_assist, get_definition_location
from rope.contrib.findit import find_occurrences

Basic Usage

from 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()

Architecture

Rope follows a layered architecture designed for safety and extensibility:

  • Project Layer: Manages Python projects, resources, and configuration
  • PyCore Layer: Provides Python code analysis and object model
  • Refactoring Layer: Implements safe code transformations with change management
  • Contrib Layer: Offers IDE integration tools and code assistance features

The design emphasizes safety through static analysis, avoiding runtime execution of analyzed code, making it suitable for IDE integration and automated refactoring tools.

Capabilities

Project Management

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(): ...

Project Management

Refactoring Operations

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): ...

Refactoring Operations

Code Assistance

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): ...

Code Assistance

Change Management

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): ...

Change Management

Error Handling

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): ...

Error Handling

Types

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): ...