Module

compiler.statements.functions

Function statement compilation for Kida compiler.

Provides mixin for compiling function statements (def, call_block, slot).

Uses inline TYPE_CHECKING declarations for host attributes. See: plan/rfc-mixin-protocol-typing.md

Classes

FunctionCompilationMixin 13
Mixin for compiling function statements. Host attributes and cross-mixin dependencies are declared…

Mixin for compiling function statements.

Host attributes and cross-mixin dependencies are declared via inline TYPE_CHECKING blocks.

Methods

Internal Methods 13
_parse_annotation 1 ast.expr | None
Convert a raw annotation string to a Python AST expression node. Uses ``ast.pa…
staticmethod
def _parse_annotation(raw: str) -> ast.expr | None

Convert a raw annotation string to a Python AST expression node.

Usesast.parsein eval mode to parse annotation text like "str | None"into the corresponding AST. Falls back to None for malformed annotations.

Parameters
Name Type Description
raw

Annotation text from the template source.

Returns
ast.expr | None AST expression node, or None if parsing fails.
_make_callable_preamble 1 list[ast.stmt]
Build common local runtime preamble for callable codegen paths.
def _make_callable_preamble(self, *, include_scope_stack: bool = False) -> list[ast.stmt]
Parameters
Name Type Description
include_scope_stack Default:False
Returns
list[ast.stmt]
_make_def_runtime_setup 1 list[ast.stmt]
Build the callable preamble, local context, and slot-presence helper.
def _make_def_runtime_setup(self, plan: CallableSignaturePlan) -> list[ast.stmt]
Parameters
Name Type Description
plan
Returns
list[ast.stmt]
_make_def_component_stack_scaffold 1 tuple[ast.Expr, ast.Expr]
Build paired component-stack push and pop statements for one def.
staticmethod
def _make_def_component_stack_scaffold(def_name: str) -> tuple[ast.Expr, ast.Expr]
Parameters
Name Type Description
def_name
Returns
tuple[ast.Expr, ast.Expr]
_compile_def 1 list[ast.stmt]
Compile {% def name(params) %}...{% enddef %. Kida functions have true lexical…
def _compile_def(self, node: Def) -> list[ast.stmt]

Compile {% def name(params) %}...{% enddef %.

Kida functions have true lexical scoping - they can access variables from their enclosing scope, unlike Jinja2 macros.

Generates:

def _def_name(arg1, arg2=default, *, _caller=None, _outer_ctx=ctx):
    buf = []
    ctx = {**_outer_ctx, 'arg1': arg1, 'arg2': arg2}
    if _caller:
        ctx['caller'] = _caller
    ... body ...
    return Markup(''.join(buf))
ctx['name'] = _def_name
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_make_call_slot_function 1 ast.FunctionDef
Lower one named call-block slot into its callback function.
def _make_call_slot_function(self, slot: CallSlotPlan) -> ast.FunctionDef
Parameters
Name Type Description
slot
Returns
ast.FunctionDef
_make_caller_wrapper_body 0 list[ast.stmt]
Build caller dispatch with temporary source-location context.
staticmethod
def _make_caller_wrapper_body() -> list[ast.stmt]
Returns
list[ast.stmt]
_make_call_wrapper_scaffold 1 list[ast.stmt]
Build slot dispatch mapping, source captures, wrapper, and bound lambda.
def _make_call_wrapper_scaffold(self, plan: CallBlockPlan) -> list[ast.stmt]
Parameters
Name Type Description
plan
Returns
list[ast.stmt]
_compile_call_with_caller 1 ast.Call
Lower a raw call expression and attach the bound caller callback.
def _compile_call_with_caller(self, call: Node) -> ast.Call
Parameters
Name Type Description
call
Returns
ast.Call
_compile_call_block 1 list[ast.stmt]
Compile {% call func(args) %}...{% endcall %. Builds a caller that supports na…
def _compile_call_block(self, node: CallBlock) -> list[ast.stmt]

Compile {% call func(args) %}...{% endcall %.

Builds a caller that supports named slots. Passes _caller(slot="default") so both _caller() and _caller("header_actions") work.

With scoped slots: slot functions accept **_slot_kwargs from the def-side bindings and push them onto the scope stack so let: params are available as local variables in the slot body.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_make_region_function 2 tuple[list[ast.FunctionD…
Generate module-level region callable: _region_name(params, *, _outer_ctx) -> M…
def _make_region_function(self, name: str, node: Region) -> tuple[list[ast.FunctionDef], ast.FunctionDef]

Generate module-level region callable: _region_name(params, *, _outer_ctx) -> Markup.

Regions compile to both a callable (this) and a block wrapper. No _caller/slots — regions are parameterized renderable units only. Returns (thunk_defs, region_func) — thunks must be emitted before the region func.

Parameters
Name Type Description
name
node
Returns
tuple[list[ast.FunctionDef], ast.FunctionDef]
_compile_region 1 list[ast.stmt]
Compile {% region name(params) %} for template body — emit ctx assign only.
def _compile_region(self, node: Region) -> list[ast.stmt]
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_slot 1 list[ast.stmt]
Compile {% slot %} or {% slot name %} with optional scoped bindings. Renders t…
def _compile_slot(self, node: Slot) -> list[ast.stmt]

Compile {% slot %} or {% slot name %} with optional scoped bindings.

Renders the caller content for the given slot name. _caller(slot="default") supports both _caller() and _caller("name").

With scoped bindings: {% slot row let:item=item, let:index=loop.index %}

Passes bindings as keyword arguments to the caller function:

_caller("row", item=item, index=loop.index)

With body (default content): renders body when no caller is present.

Parameters
Name Type Description
node
Returns
list[ast.stmt]