HNHacker News
TopNewBestAskShowJobs

turbopython

3 karma · joined October 5, 2026

submissionscomments
turbopython··on TurboPython – A Python-to-C++ Compiler
Recursive functions are handled partially ATM, for common cases like find(l, i) where the function is called recursively with the same argument. The CFG based approach should solve that by walking a call graph until fixpoint (with some budget). For body-less functions (ones that are defined in C++, e.g. builtin functions like min/max) the language has annotations to provide such information:

    @cpp_template("::tpy::min_key({0}, {1}, {2})", borrows=("a", "b"))
    def min[T, K: Comparable](a: T, b: T, key: Fn[[T], K]) -> T: ...
(the result may borrow either a or b)

Current approach is to warn because the checks are less precise than what I'd like them to. The idea is to keep it that way for now, migrate to CFG in the future and enable it in a strict mode.

turbopython··on TurboPython – A Python-to-C++ Compiler
Author here: you're right, it is a separate language that keeps Python's syntax (it uses Python's ast module for parsing) and as much of its semantics as possible. It does have extensions, but limited to new type annotations and decorators, for things like ownership or dynamic dispatch. It builds to a standalone native binary, but can also be used to compile CPython extension modules.

The source is valid Python though, with compiler warnings where the behavior diverges. In fact that's how it's tested: about 90% of the test cases are run through both TurboPython and CPython, and the output must be byte-identical. However, usually it doesn't work the other way around: not every Python program will pass TurboPython compilation, most will require adjustments to adhere to its stricter rules.

Example: https://github.com/trozen/tpy-lang/blob/master/tests/cases/a... (when run via CPython, the tpy package is a stub implementation of TurboPython's core types)

turbopython··on TurboPython – A Python-to-C++ Compiler
Author here: there's no notation, the compiler infers that from function body. If there are more parameters, the result is just an union of those, for example:

    def choose(a: list[Reading], b: list[Reading], choose_a: bool) -> Reading:
        if choose_a:
            return a[0]
        return b[0]
the return borrows from either a or b, the caller gets a warning when b is modified while the borrow is held. OTOH returning a reference to an local object is an error; locals can be returned by ownership-passing (Own[Reading] -- NRVO in C++). Currently the analysis happens on AST level, so it has it's drawback. I'm working on a CFG based MIR, to make this less coarse, though that's still a while since it's ready.