Skip to content

Fix undeclared variables in nested list comprehension with strict_undefined #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/build/unreleased/418.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. change::
:tags: bug, parser
:tickets: 418

Fix undefined variable errors when strict_undefined is True and using
nested list comprehension.

2 changes: 2 additions & 0 deletions mako/pyparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def visit_FunctionDef(self, node):
def visit_ListComp(self, node):
if self.in_function:
for comp in node.generators:
self.visit(comp.target)
self.visit(comp.iter)
else:
self.generic_visit(node)
Expand All @@ -102,6 +103,7 @@ def visit_ListComp(self, node):
def visit_DictComp(self, node):
if self.in_function:
for comp in node.generators:
self.visit(comp.target)
self.visit(comp.iter)
else:
self.generic_visit(node)
Expand Down
13 changes: 13 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,19 @@ def foo():

eq_(result_lines(t.render()), ["foo"])

def test_nested_list_comprehensions_in_function_plus_undeclared_strict(
self,
):
t = Template(
"""
<%foo = lambda b : sum(f for s in b for f in s)%>\
${ foo(([1,2],[3,4]))}
""",
strict_undefined=True,
)

eq_(result_lines(t.render()), ["10"])


class StopRenderingTest(TemplateTest):
def test_return_in_template(self):
Expand Down