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 4
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 4
_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.
_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]
_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.

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_slot 1 list[ast.stmt]
Compile {% slot %} or {% slot name %. Renders the caller content for the given…
def _compile_slot(self, node: Slot) -> list[ast.stmt]

Compile {% slot %} or {% slot name %.

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

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