This guide demonstrates how to analyze and manipulate type annotations with Codegen SDK.

Common use cases include:

  • Adding a type to a union or generic type
  • Checking if a generic type has a given subtype
  • Resolving a type annotation

Adding type hints can improve developer experience and significantly speed up programs like the Typescript compiler and mypy.

See Type Annotations for a general overview of the type maninpulation

APIs for monitoring types

Codegen programs typically access type annotations through the following APIs:

Each of these has an associated setter.

Finding the extent of your type coverage

To get an indication of your progress on type coverage, analyze the percentage of typed elements across your codebase

# Initialize counters for parameters
total_parameters = 0
typed_parameters = 0

# Initialize counters for return types
total_functions = 0
typed_returns = 0

# Initialize counters for class attributes
total_attributes = 0
typed_attributes = 0

# Count parameter and return type coverage
for function in codebase.functions:
    # Count parameters
    total_parameters += len(function.parameters)
    typed_parameters += sum(1 for param in function.parameters if param.is_typed)

    # Count return types
    total_functions += 1
    if function.return_type and function.return_type.is_typed:
        typed_returns += 1

# Count class attribute coverage
for cls in codebase.classes:
    for attr in cls.attributes:
        total_attributes += 1
        if attr.is_typed:
            typed_attributes += 1

# Calculate percentages
param_percentage = (typed_parameters / total_parameters * 100) if total_parameters > 0 else 0
return_percentage = (typed_returns / total_functions * 100) if total_functions > 0 else 0
attr_percentage = (typed_attributes / total_attributes * 100) if total_attributes > 0 else 0

# Print results
print("\nType Coverage Analysis")
print("---------------------")
print(f"Parameters: {param_percentage:.1f}% ({typed_parameters}/{total_parameters} typed)")
print(f"Return types: {return_percentage:.1f}% ({typed_returns}/{total_functions} typed)")
print(f"Class attributes: {attr_percentage:.1f}% ({typed_attributes}/{total_attributes} typed)")

This analysis gives you a breakdown of type coverage across three key areas:

  1. Function parameters - Arguments passed to functions
  2. Return types - Function return type annotations
  3. Class attributes - Type hints on class variables

Focus first on adding types to the most frequently used functions and classes, as these will have the biggest impact on type checking and IDE support.

Adding simple return type annotations

To add a return type, use function.set_return_type. The script below will add a -> None return type to all functions that contain no return statements:

Coming Soon: Advanced Type Inference

Codegen is building out an API for direct interface with tsc and mypy for precise type inference. Interested piloting this API? Let us know!

Was this page helpful?