Skip to content

Commit eece129

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "Fix support comprehensions inside functions when use strict_undefined…" into main
2 parents 042a63f + 30a6223 commit eece129

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

doc/build/unreleased/398.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, parser
3+
:tickets: 398
4+
5+
Fixed regression caused by the fix for :ticket:`320` where new logic added
6+
to interpret list and dictionary comprehensions would fail for expression
7+
oriented keys. As the parsing in question was not necessary for these
8+
keys, it's been removed. Pull request courtesy Sébastien Granjoux.

mako/pyparser.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ def visit_FunctionDef(self, node):
9292

9393
def visit_ListComp(self, node):
9494
if self.in_function:
95-
if not isinstance(node.elt, _ast.Name):
96-
self.visit(node.elt)
9795
for comp in node.generators:
9896
self.visit(comp.iter)
9997
else:
@@ -103,8 +101,6 @@ def visit_ListComp(self, node):
103101

104102
def visit_DictComp(self, node):
105103
if self.in_function:
106-
if not isinstance(node.key, _ast.Name):
107-
self.visit(node.elt)
108104
for comp in node.generators:
109105
self.visit(comp.iter)
110106
else:

test/test_template.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,21 @@ def test_list_comprehensions_plus_undeclared_strict(self):
878878

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

881+
def test_dict_comprehensions_in_function_plus_undeclared_strict(self):
882+
t = Template(
883+
"""
884+
<%
885+
def foo():
886+
return {s[0]: s for s in ('foo',)}
887+
%>
888+
889+
${ foo()['f'] }
890+
""",
891+
strict_undefined=True,
892+
)
893+
894+
eq_(result_lines(t.render()), ["foo"])
895+
881896

882897
class StopRenderingTest(TemplateTest):
883898
def test_return_in_template(self):

0 commit comments

Comments
 (0)