Type Resolution
Codegen builds a complete dependency graph of your codebase, connecting functions, classes, imports, and their relationships. This enables powerful type resolution capabilities:Type resolution follows imports and handles complex cases like type aliases, forward references, and generic type parameters.
Core Interfaces
Type annotations in Codegen are built on two key interfaces:- Typeable - The base interface for any node that can have a type annotation (parameters, variables, functions, etc). Provides
.type
and.is_typed
. - Type - The base class for all type annotations. Provides type resolution and dependency tracking.
Typeable
will have a .type
property that returns a Type
object, which can be used to inspect and modify type annotations.
Learn more about inheritable behaviors like Typeable here
Core Type APIs
Type annotations can be accessed and modified through several key APIs:Function Types
The main APIs for function types are Function.return_type and Function.set_return_type:Parameter Types
Parameters use Parameter.type and Parameter.set_type_annotation:Variable Types
Variables and attributes use Assignment.type and Assignment.set_type_annotation. This applies to:- Global variables
- Local variables
- Class attributes (via Class.attributes)
Working with Complex Types
Union Types
Union types (UnionType) can be manipulated as collections:Learn more about working with collections here
Generic Types
Generic types (GenericType) expose their parameters as collection of Parameters:Learn more about working with collections here