Skip to main content
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
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!