Codegen provides first-class support for both Python and TypeScript codebases. The language is automatically inferred when you initialize a codebase.
Language Detection
When you create a new Codebase
instance, Codegen automatically detects the programming language:
from codegen import Codebase
# Automatically detects Python or TypeScript
codebase = Codebase("./")
# View language with `codebase.language`
print(codebase.language) # "python" or "typescript"
Type System
Codegen uses specialized types for each language. These are defined as type aliases:
# Python codebases use PyCodebaseType
PyCodebaseType = Codebase[
PyFile, Directory, PySymbol, PyClass, PyFunction,
PyImport, PyAssignment, Interface, TypeAlias,
PyParameter, PyCodeBlock
]
# TypeScript codebases use TSCodebaseType
TSCodebaseType = Codebase[
TSFile, Directory, TSSymbol, TSClass, TSFunction,
TSImport, TSAssignment, TSInterface, TSTypeAlias,
TSParameter, TSCodeBlock
]
Every code element has both a Python and TypeScript implementation that inherits from a common base class. For example:
…
# Base class (core/function.py)
class Function:
"""Abstract representation of a Function."""
pass
# Python implementation (python/function.py)
class PyFunction(Function):
"""Extends Function for Python codebases."""
pass
# TypeScript implementation (typescript/function.py)
class TSFunction(Function):
"""Extends Function for TypeScript codebases."""
pass
This inheritance pattern means that most Codegen programs can work with either Python or TypeScript without modification, since they share the same API structure.
# Works for both Python and TypeScript
for function in codebase.functions:
print(f"Function: {function.name}")
print(f"Parameters: {[p.name for p in function.parameters]}")
print(f"Return type: {function.return_type}")
TypeScript-Specific Features
Some features are only available in TypeScript codebases:
Example of TypeScript-specific features:
# Only works with TypeScript codebases
if isinstance(codebase, TSCodebaseType):
# Work with TypeScript interfaces
for interface in codebase.interfaces:
print(f"Interface: {interface.name}")
print(f"Extends: {[i.name for i in interface.parent_interfaces]}")
# Work with type aliases
for type_alias in codebase.type_aliases:
print(f"Type alias: {type_alias.name}")