The typhon command
Run, inspect and build single Typhon files from the command line, and control the script cache and the compile server.
The typhon command handles single .ty files the way python handles .py files. It also shows the generated C# and builds assemblies or native executables. Quick start shows how to publish it.
The command compiles a script and the sibling modules it imports against the .NET base class library, the Typhon runtime and Typhon.NumPy. A script that imports from ASP.NET Core also gets the ASP.NET Core framework, and a script can name NuGet packages, as described below. Code that needs other projects goes into a project, which dotnet build compiles.
Commands
| Command | Effect |
|---|---|
typhon run <file.ty> [args...] | Compiles the file in memory and runs it with the given arguments. |
typhon emit <file.ty> | Prints the generated C#. |
typhon check <file.ty> | Compiles the file and reports diagnostics without running it. |
typhon build <file.ty> -o <dir> | Writes <dir>/typhon_<name>.dll with the files it needs. dotnet <dir>/typhon_<name>.dll runs it. |
typhon build <file.ty> --aot -o <dir> | Writes the self-contained native executable <dir>/typhon_<name>. Native AOT needs the platform’s C++ build tools, on Linux clang and zlib1g-dev. It does not support ASP.NET Core programs. |
typhon test [file.ty or dir ...] | Runs pytest-style tests and reports like pytest. |
typhon server start, stop, status | Controls the background compile server. |
Options
| Option | Effect |
|---|---|
--strict, --permissive | Sets the type mode. The default is warn. |
--bigint | Compiles int as an unbounded BigInteger for the whole program. |
--time | Writes timings to standard error, including the path typhon run took. |
--no-cache | Compiles even if the script cache holds a build of the file. TYPHON_CACHE=0 has the same effect. |
--no-server | Compiles in the current process. TYPHON_SERVER=0 has the same effect. |
Path selection in typhon run
typhon run takes one of three paths, and each produces the same program:
- If the script ran before and neither the file, an imported module nor the compiler build has changed since, the script cache supplies the compiled program. The cache lives in the user’s cache directory, and
TYPHON_CACHE_DIRmoves it. Entries that stay unused for 30 days are removed. - Otherwise the background compile server compiles the script. The server keeps a warm compiler in memory. There is one server per user and CLI build, only that user can reach it, and it exits after 10 idle minutes.
- If no server is running,
typhon runcompiles in its own process and starts a server for the next run.
Scripts compiled by typhon run reference a precompiled copy of the Typhon runtime (src/Typhon.Runtime). Builds made with typhon build and projects compiled by the source generator contain the runtime themselves.
ASP.NET Core
A script that imports from Microsoft.AspNetCore or Microsoft.Extensions also references the ASP.NET Core shared framework. The command uses the installed version that matches the major and minor version of the .NET runtime it runs on. A web API therefore needs no project file:
typhon run samples/MinimalApi/main.ty --urls http://localhost:5000
typhon build samples/MinimalApi/main.ty -o out/api
dotnet out/api/typhon_main.dll --urls http://localhost:5000
A script that uses the framework without importing from it can ask for it with the line #:sdk Microsoft.NET.Sdk.Web, as a .NET file-based app does. Python reads the line as a comment. Other scripts do not reference the framework. If the ASP.NET Core runtime is not installed, the command reports it. typhon build --aot refuses ASP.NET Core programs, because Native AOT for minimal APIs needs a full project.
NuGet packages
A script names the NuGet packages it needs in #:package lines, the directive of .NET file-based apps. Python reads these lines as comments, so the file stays valid Python:
#:package Humanizer.Core@2.14.1
#:package Newtonsoft.Json@13.0.3
from Humanizer import NumberToWordsExtension
from Newtonsoft.Json import JsonConvert
print(NumberToWordsExtension.ToWords(1234), JsonConvert.SerializeObject([1, 2, 3]))
The first typhon run, check or build restores the packages and their dependencies into the cache folder, which needs the .NET SDK and access to the package feed. A version of @*, or none, takes the latest version, which is resolved again once a day. typhon build copies the packages’ assemblies next to the program.
Standard library from CPython’s source
Typhon’s runtime implements the standard library modules that programs use most, in C#. A script with a #:pylib line can also import other modules: a module that neither the script’s folder nor Typhon’s runtime has is compiled from CPython’s own source, Lib/<name>.py, together with the program. The feature is experimental.
#:pylib
import graphlib
import colorsys
ts = graphlib.TopologicalSorter({"b": {"a"}, "c": {"b"}})
print(list(ts.static_order()), colorsys.rgb_to_hsv(0.2, 0.4, 0.4))
Such a module is compiled like a module of the program, typed by the program’s calls and kept in the compile cache. Typhon looks for Lib/<name>.py and for packages with Lib/<name>/__init__.py, and follows their submodules and relative imports. Typhon’s own modules take precedence, with two exceptions under #:pylib: doctest comes from CPython’s source and runs the examples of a module, and a script that imports ast, opcode or dis itself gets CPython’s source of that module under the same name. If only other modules import them, they get Typhon’s implementation. A #:pylib-source line names further modules that come from CPython’s source under the same import name, separated by spaces or commas, for example #:pylib-source gzip, pickle. Their source comes from the same Lib folder, but other modules that the runtime lacks come from that folder only with a #:pylib line. The Lib folder comes from #:pylib DIR, the environment variable TYPHON_PYLIB, or the standard library of the python3 on the PATH. Whether a module compiles also depends on the CPython version of that library, and Typhon’s tests use CPython 3.15.
Typhon also ships the unchanged sources of some CPython 3.15.2 modules, among them cmd, decimal, netrc, optparse, pstats, selectors and token, together with modules they depend on. Programs can import them without a Lib folder or a #:pylib line, also in programs built with typhon build. Each of these files contains CPython’s attribution and license in its header.
Tests
typhon test collects the files named test_*.ty or *_test.ty, below the current directory unless files or directories are given. It runs a module’s body first and then its test* functions and the test* methods of its Test* classes, in the order they are defined. A test module imports pytest for fixtures, mark.parametrize, skip, skipif, xfail, raises and approx, and the fixtures tmp_path and capsys are built in.
The report follows pytest’s: one character per test, then each failure with its assert line, the compared values and the test’s output, and a summary. The exit code is 1 if a test failed and 5 if no test was collected. typhon run of a test module runs only its body, as python does.
Measured run times
These whole-process times come from the benchmark report and were measured on Windows, on battery, with the CLI published as ReadyToRun:
| Situation | Tiny script | N-body |
|---|---|---|
CPython, python script.py | 30 ms | 3,283 ms |
First typhon run, compiling in its own process | 642 ms | 721 ms |
Later typhon run, compiled by the warm server | 84 ms | 146 ms |
typhon run of an unchanged script from the cache | 80 ms | 126 ms |
Program built with typhon build, per run | 45 ms | 95 ms |
| Native AOT executable, per run | 23 ms | 78 ms |
CPython starts tiny scripts faster than any typhon run path. For N-body, the first typhon run, compilation included, takes less than a quarter of CPython’s time. The benchmark page has the full table.