Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python Backend

Second target. Status: v2 — verified; mypy --strict is clean and the full conformance suite passes in pytest.

Representation

LTPython
Int (i64)int + _lt.iadd/isub/imul/idiv/irem/ineg (64-bit mask)
Floatfloat; division uses _lt.fdiv so x / 0.0 = ±inf/NaN
Strstr (indexed per code point)
Boolbool
List<T>_lt.LList (immutable tuple)
Map<K,V>_lt.LMap (entries ordered by cmp, lookup via binary search)
Dict<V>_lt.LDict (str keys, insertion order)
fn(A) -> RCallable[[A], R]; a lambda becomes a nested def
Valueprelude enum value; _lt.json_parse/json_stringify
Set<T>_lt.LSet
Option<T>_lt.Opt (tag = "some"/"none", values tuple)
Result<T,E>_lt.Res (tag = "ok"/"err")
enum@dataclass(frozen=True) with tag/values/order; variants are Shape.circle(...) (@staticmethod) and Shape.Point (ClassVar); display via to_display
struct@dataclass(frozen=True); fields become named constructor parameters, plus lt_cmp and to_display; embed fields remain regular fields and widens to users.table_info
package-level functionsdef name(...) (export) / def _name(...) (private)
package bindingsname = … / _name = …
packageone module: gen/<package>.py
receiver functionsremain module functions; p.len() becomes len(p) when lowering
Display_lt.disp(value); to_display calls the package’s to_string when present
Eq / Ord_lt.eq, @dataclass __eq__, _lt.cmp
?if t.tag == "err"/"none": return t
templateconcatenation + _lt.disp(...)
tagged templatecall to a package-level function with LList[str] + LList[T]; generic enum type arguments are erased to Any
matchif/elif chain on value.tag
JSXconstructor call with keyword arguments
expect.eq etc._lt.expect_equal / expect_not_equal / expect_true / expect_false
test "title"def test_title() -> None in tests/test_<package>.py

Builtin shadowing: a package that declares a function named int/str/list/… makes primitive annotations in that module written as builtins.int so mypy --strict stays valid.

Package structure

out/py/
  lt_runtime.py
  pyproject.toml          # pytest: pythonpath=["."], testpaths=["tests"]
  gen/__init__.py
  gen/<package>.py        # one module per LT package
  gen/<dir>/__init__.py   # for packages in subdirectories
  tests/test_<package>.py # only from `lt test`

Cross-package imports: import gen.models.user as m1; cross-module references use that alias. Private helpers can still be called by tests through m1._helper.

Test

lt test -t py tests/conformance/*.lt --out out/conformance --run

--run runs python3 -m pytest -q. The scripts/conformance-py.sh script creates a virtualenv if none exists, runs mypy --strict ., then pytest; dependencies are installed only if they are not already available.

Limitations

  • Generic type parameters are erased in Python (duck typing); generic structs/enums become regular classes.
  • Int is unbounded in Python; masking happens only on emitted arithmetic operations.
  • Ordering is not exposed on the language surface yet.
  • Builtins are only available as receivers (xs.map(…)), not list.map(xs, …).