Skip to content

Fix Transformer_NonRecursive crashing when root node is discarded#1614

Open
gaoflow wants to merge 1 commit into
lark-parser:masterfrom
gaoflow:fix-transformer-nonrecursive-discard-root
Open

Fix Transformer_NonRecursive crashing when root node is discarded#1614
gaoflow wants to merge 1 commit into
lark-parser:masterfrom
gaoflow:fix-transformer-nonrecursive-discard-root

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Bug

Transformer_NonRecursive.transform() raises ValueError: not enough values to unpack (expected 1, got 0) when the root tree node's callback returns Discard.

from lark import Lark
from lark.visitors import Transformer_NonRecursive, Discard

class T(Transformer_NonRecursive):
    def start(self, children):
        return Discard

g = Lark('start: A+\nA: /[a-z]+/', lexer='basic')
tree = g.parse('hello')
T().transform(tree)  # ValueError: not enough values to unpack (expected 1, got 0)

The regular Transformer handles this correctly by returning None when the root is discarded (see Transformer.transform() lines 161–163).

Fix

Add the same empty-stack guard to Transformer_NonRecursive.transform() before the destructuring assignment.

Two-line change in lark/visitors.py + a regression test in tests/test_trees.py.

…scarded

When the root tree node's callback returns Discard, the stack is left
empty and the unpacking `result, = stack` raises ValueError. The
recursive Transformer.transform() handles this by returning None;
apply the same guard here.

Fixes lark-parser#1611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant