Function Calls and Call Sites
Codegen provides comprehensive APIs for working with function calls through several key classes:
- FunctionCall - Represents a function invocation
- Argument - Represents arguments passed to a function
- Parameter - Represents parameters in a function definition
See Migrating APIs for relevant tutorials and applications.
Navigating Function Calls
Codegen provides two main ways to navigate function calls:
- From a function to its call sites using call_sites
- From a function to the calls it makes (within it’s CodeBlock) using function_calls
Here’s how to analyze function usage patterns:
This navigation allows you to:
- Find heavily used functions
- Analyze call patterns
- Map dependencies between functions
Arguments and Parameters
The Argument class represents values passed to a function, while Parameter represents the receiving variables in the function definition:
Consider the following code:
You can access and modify the arguments and parameters of the function call with APIs detailed below.
Finding Arguments
The primary APIs for finding arguments are:
Modifying Arguments
There are two ways to modify function call arguments:
- Using FunctionCall.set_kwarg(…) to add or modify keyword arguments:
- Using FuncionCall.args.append(…) to add new arguments:
FunctionCall.args is a Collection of Argument objects, so it supports .append(…), .insert(…) and other collection methods.
The set_kwarg
method provides intelligent argument manipulation:
- If the argument exists and is positional, it converts it to a keyword argument
- If the argument exists and is already a keyword, it updates its value (if override_existing=True)
- If the argument doesn’t exist, it creates it (if create_on_missing=True)
- When creating new arguments, it intelligently places them based on parameter order
Arguments and parameters support safe edit operations like so:
Finding Function Definitions
Every FunctionCall can navigate to its definition through function_definition and function_definitions:
Finding Parent (Containing) Functions
FunctionCalls can access the function that invokes it via parent_function.
For example, given the following code:
You can find the parent function of the helper call:
Method Chaining
Codegen enables working with chained method calls through predecessor and related properties:
For example, for the following database query:
You can access the chain of calls:
Was this page helpful?