Skip to content

Commit

Permalink
Fix support comprehensions inside functions when use strict_undefined…
Browse files Browse the repository at this point in the history
… flag

Fixes: #398

The element or key and value used respectively in list/set comprehension
and dictionary comprehension cannot be used to declare identifiers so no
need to visit them.
  • Loading branch information
Sébastien Granjoux committed May 13, 2024
1 parent af80cbd commit a52802b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 0 additions & 4 deletions mako/pyparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ def visit_FunctionDef(self, node):

def visit_ListComp(self, node):
if self.in_function:
if not isinstance(node.elt, _ast.Name):
self.visit(node.elt)
for comp in node.generators:
self.visit(comp.iter)
else:
Expand All @@ -103,8 +101,6 @@ def visit_ListComp(self, node):

def visit_DictComp(self, node):
if self.in_function:
if not isinstance(node.key, _ast.Name):
self.visit(node.elt)
for comp in node.generators:
self.visit(comp.iter)
else:
Expand Down
15 changes: 15 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,21 @@ def test_list_comprehensions_plus_undeclared_strict(self):

eq_(result_lines(t.render(t="T")), ["t is: T", "a,b,c"])

def test_dict_comprehensions_in_function_plus_undeclared_strict(self):
t = Template(
"""
<%
def foo():
return {s[0]: s for s in ('foo',)}
%>
${ foo()['f'] }
""",
strict_undefined=True,
)

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


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

0 comments on commit a52802b

Please sign in to comment.