Classes
FStringCoalescingMixin
7
▼
Mixin for f-string coalescing optimization.
Host attributes and cross-mixin dependencies are decla…
FStringCoalescingMixin
7
▼
Mixin for f-string coalescing optimization.
Host attributes and cross-mixin dependencies are declared via inline TYPE_CHECKING blocks.
Methods
Internal Methods 7 ▼
_get_pure_filters
0
frozenset[str]
▼
Get combined set of built-in and user-defined pure filters.
_get_pure_filters
0
frozenset[str]
▼
def _get_pure_filters(self) -> frozenset[str]
Returns
frozenset[str]
_is_coalesceable
1
bool
▼
Check if node can be coalesced into an f-string.
Coalesceable nodes:
- Dat…
_is_coalesceable
1
bool
▼
def _is_coalesceable(self, node: Any) -> bool
Check if node can be coalesced into an f-string.
Coalesceable nodes:
- Data nodes (literal text)
- Output nodes with simple expressions
Non-coalesceable nodes:
- Control flow (If, For, While, etc.)
- Output with complex expressions (function calls, etc.)
- Any node containing backslashes in string constants
Parameters
| Name | Type | Description |
|---|---|---|
node |
— |
Returns
bool
_is_simple_output
1
bool
▼
Check if Output node is simple enough for f-string.
_is_simple_output
1
bool
▼
def _is_simple_output(self, node: Any) -> bool
Parameters
| Name | Type | Description |
|---|---|---|
node |
— |
Returns
bool
_is_simple_expr
1
bool
▼
Recursively check if expression is simple enough for f-string.
Simple expressi…
_is_simple_expr
1
bool
▼
def _is_simple_expr(self, expr: Any) -> bool
Recursively check if expression is simple enough for f-string.
Simple expressions:
- Constants (strings, numbers, booleans)
- Names (variable references)
- Attribute access (name.attr, name.attr.subattr)
- Item access (name[key], name["key"])
- Pure filters with simple arguments
- Pipelines with all pure steps
- InlinedFilter (method calls like .upper())
Complex expressions (NOT coalesceable):
- Function calls (may have side effects)
- Ternary expressions (complex control flow)
- Binary/unary ops (complex evaluation)
- Expressions containing backslashes
Parameters
| Name | Type | Description |
|---|---|---|
expr |
— |
Returns
bool
_expr_contains_backslash
1
bool
▼
Check if expression would generate code with backslashes.
F-strings cannot con…
_expr_contains_backslash
1
bool
▼
def _expr_contains_backslash(self, expr: Any) -> bool
Check if expression would generate code with backslashes.
F-strings cannot contain backslashes in expression parts. This is a Python syntax limitation.
Parameters
| Name | Type | Description |
|---|---|---|
expr |
— |
Returns
bool
_compile_coalesced_output
1
ast.stmt
▼
Generate f-string append for coalesced nodes.
Note on brace handling:
ast.…
_compile_coalesced_output
1
ast.stmt
▼
def _compile_coalesced_output(self, nodes: list[Any]) -> ast.stmt
Generate f-string append for coalesced nodes.
Note on brace handling:
ast.JoinedStr automatically handles brace escaping when the AST
is compiled to bytecode. We do NOT manually escape {{ and }}.
Literal text goes into ast.Constant nodes as-is.
Expressions go into ast.FormattedValue nodes.
Note on backslashes:
F-strings cannot contain backslashes in expression parts.
We detect backslashes during coalesceable checking and fall back.
Parameters
| Name | Type | Description |
|---|---|---|
nodes |
— |
Returns
ast.stmt
_compile_body_with_coalescing
1
list[ast.stmt]
▼
Compile template body with f-string output coalescing.
Groups consecutive coal…
_compile_body_with_coalescing
1
list[ast.stmt]
▼
def _compile_body_with_coalescing(self, nodes: list[Any]) -> list[ast.stmt]
Compile template body with f-string output coalescing.
Groups consecutive coalesceable nodes and generates single f-string appends for groups of 2+ nodes. Falls back to normal compilation for single nodes or non-coalesceable nodes.
Parameters
| Name | Type | Description |
|---|---|---|
nodes |
— |
List of template AST nodes to compile |
Returns
list[ast.stmt]
List of Python AST statements