Skip to content

Commit 0fb93ba

Browse files
Sébastien Granjouxzzzeek
Sébastien Granjoux
authored andcommitted
Fix undeclared variables in nested list comprehension with strict_undefined
Fix undefined variable errors when ``strict_undefined=True`` when using a nested list comprehension. Pull request courtesy Sébastien Granjoux. Fixes: #418 Closes: #419 Pull-request: #419 Pull-request-sha: 0b64e72 Change-Id: I13f6296ffe1f5f0d5f68b7da07ff76bbac7668e0
1 parent e6915e3 commit 0fb93ba

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/build/unreleased/418.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: bug, lexer
3+
:tickets: 418
4+
5+
Fix undefined variable errors when ``strict_undefined=True`` when using a
6+
nested list comprehension. Pull request courtesy Sébastien Granjoux.
7+

mako/pyparser.py

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def visit_FunctionDef(self, node):
9393
def visit_ListComp(self, node):
9494
if self.in_function:
9595
for comp in node.generators:
96+
self.visit(comp.target)
9697
self.visit(comp.iter)
9798
else:
9899
self.generic_visit(node)
@@ -102,6 +103,7 @@ def visit_ListComp(self, node):
102103
def visit_DictComp(self, node):
103104
if self.in_function:
104105
for comp in node.generators:
106+
self.visit(comp.target)
105107
self.visit(comp.iter)
106108
else:
107109
self.generic_visit(node)

test/test_template.py

+13
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,19 @@ def foo():
916916

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

919+
def test_nested_list_comprehensions_in_function_plus_undeclared_strict(
920+
self,
921+
):
922+
t = Template(
923+
"""
924+
<%foo = lambda b : sum(f for s in b for f in s)%>\
925+
${ foo(([1,2],[3,4]))}
926+
""",
927+
strict_undefined=True,
928+
)
929+
930+
eq_(result_lines(t.render()), ["10"])
931+
919932

920933
class StopRenderingTest(TemplateTest):
921934
def test_return_in_template(self):

0 commit comments

Comments
 (0)