Skip to content

Commit 30a6223

Browse files
Sébastien Granjouxzzzeek
authored andcommitted
Fix support comprehensions inside functions when use strict_undefined…
Fixed regression caused by the fix for 🎫`320` where new logic added to interpret list and dictionary comprehensions would fail for expression oriented keys. As the parsing in question was not necessary for these keys, it's been removed. Pull request courtesy Sébastien Granjoux. Fixes: #398 Closes: #399 Pull-request: #399 Pull-request-sha: d708e27 Change-Id: Icc253e66b99ad30b3d2353e896c8345f4e94ad9a
1 parent af80cbd commit 30a6223

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
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:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ show-source = true
8989
enable-extensions = G
9090
# E203 is due to https://github.com/PyCQA/pycodestyle/issues/373
9191
ignore =
92-
A003,
92+
A003,A005
9393
D,
9494
E203,E305,E711,E712,E721,E722,E741,
9595
N801,N802,N806,

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)