Skip to main content
This guide covers the core APIs and patterns for working with type annotations in Codegen.

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.
Any node that inherits from 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:

Working with Complex Types

Union Types

Union types (UnionType) can be manipulated as collections:

Generic Types

Generic types (GenericType) expose their parameters as collection of Parameters:

Type Resolution

Type resolution uses Type.resolved_value to get the actual symbols that a type refers to: