Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Write lunas in Python

Define luna types in Python with define_luna and handler decorators, alone or beside TypeScript lunas in one app.

Luna types can be written in Python as well as TypeScript. An app can hold either, or both, and lunas in one language call lunas in the other.

Install the Python SDK

Install it where your editor and type checker look for packages. Python lunas need CPython 3.12 or later.

luna dev and luna deploy read your Python lunas with python3. If that is an older Python, set LUNA_PYTHON to the path of a newer one.

pip install "$(luna sdk python --path)"

The config file stays TypeScript, so the app also needs the TypeScript SDK.

npm install --install-links "$(luna sdk ts --path)"

Point the config at a Python module

Name the Python module in luna.config.ts.

import { defineConfig } from "luna";

export default defineConfig({ lunas: "./lunas.py" });

Define a luna type

define_luna takes the name and the same options as in TypeScript, as keyword arguments in snake case. Each handler is a function with a decorator.

from luna import Ctx, define_luna, t, table

List = define_luna(
    "List",
    visibility="public",
    key={"id": t.string()},
    schema={"items": table({"id": t.id(), "text": t.string()})},
)


@List.handler
def add(ctx: Ctx, text: str) -> None:
    ctx.db.items.insert({"text": text})


@List.handler(query=True)
def all(ctx: Ctx):
    return ctx.db.items.find(order_by="id")

The handler’s name is the function’s name. Its arguments and result are checked against the type hints.

Hint Type
str, int, float, bool, bytes t.string(), t.int(), t.float(), t.bool(), t.bytes()
str | None = None, or any argument with a default An optional argument
Literal["low", "high"] t.enum(["low", "high"])
list[str] t.array(t.string())
A TypedDict or a dataclass t.object({ ... })
Annotated[int, t.timestamp()] The t type you name
dict, Any or no hint t.json()

Pass name="..." to the decorator to give a handler a name other than its function’s.

Handler options

The decorator takes the options a TypeScript handler spec has, in snake case.

@List.handler(errors={"Full": t.object({"limit": t.int()})})
def add_bounded(ctx: Ctx, text: str) -> None:
    if ctx.db.items.count() >= 100:
        ctx.fail("Full", {"limit": 100})
    ctx.db.items.insert({"text": text})

query, errors, exclusive, requires, shared, durable, route and description work as in Write handlers. request=True gives the handler the raw HTTP request. ctx.fail(code, data, keep=True) keeps the call’s writes.

Hooks are functions named for the hook, such as on_create or on_wake, added with @List.hook.

Call another luna

Options go on ctx.call and ctx.send themselves, and arguments are keyword arguments.

@List.handler
async def copy_to(ctx: Ctx, target: str, text: str) -> None:
    await ctx.call(List, {"id": target}, timeout="5s").add(text=text)
    ctx.send(List, {"id": target}, delay="10m").add(text=text)

Mix Python and TypeScript

Name one module for each language.

import { defineConfig } from "luna";

export default defineConfig({ lunas: ["./lunas.ts", "./lunas.py"] });

luna dev and luna deploy write luna_gen.py and luna_gen.ts, typed handles on the other language’s luna types.

from luna_gen import Wallet

@List.handler
async def pay(ctx: Ctx, amount: int) -> None:
    await Wallet.call(ctx, {"id": ctx.key["id"]}).charge(amount=amount)
import type { List } from "./luna_gen.ts";

await ctx.call<List>("List", { id: "groceries" }).add({ text: "milk" });

Next steps

Was this page helpful?