Manipulate Function Call Sites
Learn how to manipulate function call sites with Codegen
Codegen’s GraphSitter library provides powerful APIs for systematically manipulating function call sites throughout your codebase.
This guide will walk you through the basics of working with function call sites using GraphSitter.
Common use cases include:
- Updating function calls after changing function signatures
- Refactoring to use named arguments
- Adding or removing arguments from function calls
Overview
To manipulate function call sites, you first need to locate them. You can do this using the call_sites
property of a function.
Modifying Arguments
You can add, remove, or modify arguments at a call site:
Converting to Named Arguments
You can convert positional arguments to named arguments:
This will convert all positional arguments to named arguments, based on the parameter names in the function definition.
Updating Multiple Call Sites
If you need to update multiple call sites, you can do so in a loop:
Handling Different Argument Types
GraphSitter allows you to handle different types of arguments:
Best Practices
-
Commit After Major Changes: If you’re making multiple significant changes, use
codebase.commit()
between them to ensure the codebase graph is up-to-date. -
Re-fetch References: After a commit, re-fetch any file or symbol references you’re working with, as they may have become stale.
-
Handle Errors: Be prepared to handle cases where functions or call sites might not exist, or where modifications might fail due to syntax errors.
By following these guidelines, you can effectively manipulate function call sites throughout your codebase while maintaining its integrity and functionality.
Was this page helpful?