# variables

URL: /kida/api/compiler/statements/variables/
Section: statements
Description: 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

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/compiler/statements/variables/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fvariables%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fvariables%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fvariables%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Fvariables%2Findex.txt)

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

1Class1Function

## 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]`

## Functions

`_collect_target_names`

1

`list[str]`

▼

Walk a Set/Let/Export target node, returning all bound names.

`def _collect_target_names(target: Node) -> list[str]`

##### Parameters

Name
Type
Description

`target`
`Node`

##### Returns

`list[str]`
