Module

compiler.statements.variables

Variable assignment statement compilation for Kida compiler.

Provides mixin for compiling variable assignment statements (set, let, export).

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

Classes

VariableAssignmentMixin 6
Mixin for compiling variable assignment statements. Host attributes and cross-mixin dependencies a…

Mixin for compiling variable assignment statements.

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

Methods

Internal Methods 6
_compile_set 1 list[ast.stmt]
Compile {% set %} - block-scoped variable assignment. Variables assigned with …
def _compile_set(self, node: Any) -> list[ast.stmt]

Compile {% set %} - block-scoped variable assignment.

Variables assigned with {% set %} are scoped to the current block (if/for/while/etc.) and are not accessible outside the block.

Handles both single names and structural unpacking:

{% set x = value %}
{% set a, b = 1, 2 %}
{% set (a, b), c = ([1, 2], 3) %}
Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_let 1 list[ast.stmt]
Compile {% let %} - template-scoped variable assignment. Variables assigned wi…
def _compile_let(self, node: Any) -> list[ast.stmt]

Compile {% let %} - template-scoped variable assignment.

Variables assigned with {% let %} are available throughout the template. Supports structural unpacking: {% let a, b = 1, 2 %}

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_export 1 list[ast.stmt]
Compile {% export %} - export variable to outer scope. Variables assigned with…
def _compile_export(self, node: Any) -> list[ast.stmt]

Compile {% export %} - export variable to outer scope.

Variables assigned with {% export %} are promoted from inner scope (e.g., inside a loop) to the outer scope (e.g., outside the loop).

Supports structural unpacking: {% export a, b = 1, 2 %}

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_compile_block_scoped_assignment 2 list[ast.stmt]
Compile block-scoped assignment ({% set %}). Assigns to current scope: _scope_…
def _compile_block_scoped_assignment(self, target: Any, value: Any) -> list[ast.stmt]

Compile block-scoped assignment ({% set %}).

Assigns to current scope: _scope_stack[-1][name] = value If no scope exists (top level), falls back to ctx (template-scoped).

Parameters
Name Type Description
target
value
Returns
list[ast.stmt]
_compile_export_assignment 2 list[ast.stmt]
Compile export assignment ({% export %}). Export always assigns to ctx (templa…
def _compile_export_assignment(self, target: Any, value: Any) -> list[ast.stmt]

Compile export assignment ({% export %}).

Export always assigns to ctx (template scope) to ensure the variable persists after blocks end. This matches the common use case of promoting values from loops/conditionals to template scope.

Parameters
Name Type Description
target
value
Returns
list[ast.stmt]
_compile_assignment 2 list[ast.stmt]
Common logic for template-scoped assignments ({% let %}). Handles recursive st…
def _compile_assignment(self, target: Any, value: Any) -> list[ast.stmt]

Common logic for template-scoped assignments ({% let %}).

Handles recursive structural unpacking using ctx dict for all variables.

Parameters
Name Type Description
target
value
Returns
list[ast.stmt]