Browse the documentation

Overview

Typhon compiles type-annotated Python to .NET IL. This page describes the project, its current state and the structure of the documentation.

Typhon is an experimental compiler. It reads source files written in Python 3.12 syntax with type annotations and compiles them to .NET IL. The result is an ordinary .NET assembly. C# code can call it, derive from its classes and reference it like any other library, and Typhon code can import any .NET namespace.

Warning

Typhon is a proof of concept. Most of its code is generated by AI from a set of core principles and ideas, and a test harness checks the compiled programs against CPython, using existing programs and libraries. Expect missing features and bugs. The project gives no stability or support promises. The principles are listed on the status page.

A first program

A Typhon source file has the extension .ty. Its content is Python source, and import statements reach .NET namespaces as well as other Typhon modules and the standard modules that the Typhon runtime provides:

from System.Text import StringBuilder
from dataclasses import dataclass
import math


@dataclass
class Point:
    x: float
    y: float


def path_length(points: list[Point]) -> float:
    total = 0.0
    for a, b in zip(points, points[1:]):
        total += math.hypot(b.x - a.x, b.y - a.y)
    return total


if __name__ == "__main__":
    route = [Point(0, 0), Point(3, 4), Point(6, 8)]
    sb = StringBuilder()
    sb.Append(f"{len(route)} points, ")
    sb.Append(f"length {path_length(route):.1f}")
    print(sb.ToString())

typhon run route.ty compiles the file in memory and prints 3 points, length 10.0. The compiler turns Point into a CLR class with two double properties and path_length into a static method that takes a List<Point>.

The typhon command runs single scripts like this one. Typhon code can also live in a .NET project, which dotnet build and dotnet run compile like C#. The section Projects and scripts explains when to use which.

Typhon and IronPython

IronPython also runs Python code on .NET. It keeps Python’s dynamic semantics and runs at roughly the speed of CPython. Typhon requires or infers static types and compiles to the same kind of IL that the C# compiler produces. The benchmark page shows what this means for run time on small programs.

The name

The name is short for typed Python, and the file extension .ty comes from the same two letters. Type annotations are what let the compiler produce statically typed .NET code. In Greek mythology, Typhon is a giant whose lower body is made of coiled serpents and who is connected with storms. The logo of this site builds on that figure.

Structure of this documentation