Python Backend
Second target. Status: v2 — verified; mypy --strict is clean and the full conformance suite
passes in pytest.
Representation
| LT | Python |
|---|---|
Int (i64) | int + _lt.iadd/isub/imul/idiv/irem/ineg (64-bit mask) |
Float | float; division uses _lt.fdiv so x / 0.0 = ±inf/NaN |
Str | str (indexed per code point) |
Bool | bool |
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) -> R | Callable[[A], R]; a lambda becomes a nested def |
Value | prelude 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 functions | def name(...) (export) / def _name(...) (private) |
| package bindings | name = … / _name = … |
package | one module: gen/<package>.py |
| receiver functions | remain 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 |
| template | concatenation + _lt.disp(...) |
| tagged template | call to a package-level function with LList[str] + LList[T]; generic enum type arguments are erased to Any |
match | if/elif chain on value.tag |
| JSX | constructor 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. Intis unbounded in Python; masking happens only on emitted arithmetic operations.Orderingis not exposed on the language surface yet.- Builtins are only available as receivers (
xs.map(…)), notlist.map(xs, …).