> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-codegen-bot-sdk-docs-2-0.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Increasing Type Coverage

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

<Tip>
  Adding type hints can improve developer experience and [significantly speed up](https://github.com/microsoft/Typescript/wiki/Performance#using-type-annotations) programs like the Typescript compiler and `mypy`.
</Tip>

<Note>See [Type Annotations](/building-with-codegen/type-annotations) for a general overview of the type maninpulation</Note>

## APIs for monitoring types

Codegen programs typically access type annotations through the following APIs:

* [Parameter.type](/api-reference/core/Parameter#type)
* [Function.return\_type](/api-reference/python/PyFunction#return-type)
* [Assignment.type](/api-reference/core/Assignment#type)

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

```python
# 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

<Tip>
  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.
</Tip>

## 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:

<CodeGroup>
  ```python For Python
  for file in codebase.files:
      # Check if 'app' is in the file's filepath
      if "app" in file.filepath:
          # Iterate through all functions in the file
          for function in file.functions:
              # Check if the function has no return statements
              if len(function.return_statements) == 0:
                  # Set the return type to None
                  function.set_return_type("None")
  ```

  ```python For Typescript
  for file in codebase.files:
      # Check if 'app' is in the file's filepath
      if "app" in file.filepath:
          # Iterate through all functions in the file
          for function in file.functions:
              # Check if the function has no return statements
              if len(function.return_statements) == 0:
                  # Set the return type to None
                  function.set_return_type("null")
  ```
</CodeGroup>

## Coming Soon: Advanced Type Inference

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