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 7
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 7
_compile_set 1 list[ast.stmt]
Compile {% set %} - block-scoped variable assignment. Variables assigned with …
def _compile_set(self, node: Set) -> 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) %}

With ??=, assigns only if the variable is undefined or None: {% set x ??= "default" %}

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: Let) -> 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 %}

With ??=, assigns only if the variable is undefined or None: {% let x ??= "default" %}

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: Export) -> 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 %}

With ??=, exports only if the variable is undefined or None: {% export x ??= "default" %}

Parameters
Name Type Description
node
Returns
list[ast.stmt]
_wrap_coalesce_guard 2 list[ast.stmt]
Wrap assignment statements with a nullish coalesce guard. Uses _is_defined(lam…
def _wrap_coalesce_guard(self, target: Node, stmts: list[ast.stmt]) -> list[ast.stmt]

Wrap assignment statements with a nullish coalesce guard.

Uses _is_defined(lambda: _lookup_scope(ctx, _scope_stack, name)) to match kida's scope-aware variable resolution semantics. This correctly handles:

  • Variables defined in block scopes (_scope_stack)
  • Variables defined in template scope (ctx)
  • Truly undefined variables (raises UndefinedError, caught by _is_defined)
  • Explicit None values
Parameters
Name Type Description
target
stmts
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: Node, value: Node) -> 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: Node, value: Node) -> 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: Node, value: Node) -> 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]