multi_file_rename

Multi-File Rename Module.

This module provides functionality to rename functions across multiple Python files using LibCST. It identifies both function definitions and function calls and renames them to maintain consistency throughout a codebase.

Example

>>> from haive.tools.toolkits.dev.python.cst_toolkit.transformers.multi_file_rename import rename_function_in_files
>>> rename_function_in_files("/path/to/directory", "old_function_name", "new_function_name")

Classes

MultiFileRenameTransformer

Renames a function across multiple files.

Functions

rename_function_in_files(directory, old_name, new_name)

Renames a function across all .py files in a directory recursively.

Module Contents

class multi_file_rename.MultiFileRenameTransformer(old_name: str, new_name: str)

Bases: libcst.CSTTransformer

Renames a function across multiple files.

This transformer identifies and renames both function definitions and function calls to maintain consistency throughout a codebase.

old_name

The current name of the function to rename

Type:

str

new_name

The new name to give the function

Type:

str

leave_Call(original_node: libcst.Call, updated_node: libcst.Call)

Rename function calls during the AST traversal.

Parameters:
  • original_node (cst.Call) – The original function call node

  • updated_node (cst.Call) – The updated function call node

Returns:

The function call with the updated name if it matches

the target function, otherwise the original node

Return type:

cst.Call

leave_FunctionDef(original_node: libcst.FunctionDef, updated_node: libcst.FunctionDef)

Rename function definitions during the AST traversal.

Parameters:
  • original_node (cst.FunctionDef) – The original function definition node

  • updated_node (cst.FunctionDef) – The updated function definition node

Returns:

The function definition with the updated name if it matches

the target function, otherwise the original node

Return type:

cst.FunctionDef

new_name
old_name
multi_file_rename.rename_function_in_files(directory: str, old_name: str, new_name: str)

Renames a function across all .py files in a directory recursively.

This function walks through all Python files in a directory and its subdirectories, renames both function definitions and function calls, and writes the updated code back to the files.

Parameters:
  • directory (str) – Path to the directory containing Python files

  • old_name (str) – Current name of the function to rename

  • new_name (str) – New name to give the function

Raises:
  • FileNotFoundError – If the specified directory does not exist

  • IOError – If there are issues reading from or writing to the files