Skip to content
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

Fix support comprehensions inside functions when use strict_undefined… #399

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
6 changes: 6 additions & 0 deletions doc/build/unreleased/398.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. change::
:tags: bug, parser
:tickets: 398

Fixed the fix for ticket 320. It was trigerring an error in dictionary
comprehension when the key is not a simple name.
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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ show-source = true
enable-extensions = G
# E203 is due to https://github.com/PyCQA/pycodestyle/issues/373
ignore =
A003,
A003,A005
D,
E203,E305,E711,E712,E721,E722,E741,
N801,N802,N806,
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
Loading