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

Accessing an attribute or an index in a dictionnary comprehension trigger an error with the mako version 1.3.3 #398

Closed
sgranjoux opened this issue May 13, 2024 · 3 comments

Comments

@sgranjoux
Copy link

Here is an example

from mako.template import Template

text = """
<%
    mydict = { 'foo': 1 }

    def getkeys(x):
        return { k[0]: 0 for k in x.keys() }
%>

${ ','.join( getkeys(mydict) ) }
"""

tmpl = Template(text=text, strict_undefined=True)
out = tmpl.render()
print(out)

This produces the following error:

Traceback (most recent call last):
  File "/home/sgr/Programmation/mako/test3.py", line 16, in <module>
    tmpl = Template(text=text, strict_undefined=True)
  File "/home/sgr/Programmation/mako/mako/template.py", line 299, in __init__
    (code, module) = _compile_text(self, text, filename)
  File "/home/sgr/Programmation/mako/mako/template.py", line 676, in _compile_text
    source, lexer = _compile(
  File "/home/sgr/Programmation/mako/mako/template.py", line 656, in _compile
    node = lexer.parse()
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 248, in parse
    if self.match_python_block():
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 404, in match_python_block
    self.append_node(
  File "/home/sgr/Programmation/mako/mako/lexer.py", line 129, in append_node
    node = nodecls(*args, **kwargs)
  File "/home/sgr/Programmation/mako/mako/parsetree.py", line 158, in __init__
    self.code = ast.PythonCode(text, **self.exception_kwargs)
  File "/home/sgr/Programmation/mako/mako/ast.py", line 47, in __init__
    f.visit(expr)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 136, in visit
    return self.generic_visit(node)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 144, in generic_visit
    self.visit(item)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 135, in visit
    return f(node)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 91, in visit_FunctionDef
    self._visit_function(node, False)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 138, in _visit_function
    self.visit(n)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 136, in visit
    return self.generic_visit(node)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 146, in generic_visit
    self.visit(value)
  File "/home/sgr/Programmation/mako/mako/_ast_util.py", line 135, in visit
    return f(node)
  File "/home/sgr/Programmation/mako/mako/pyparser.py", line 107, in visit_DictComp
    self.visit(node.elt)
AttributeError: 'DictComp' object has no attribute 'elt'

I am running Python 3.10.12 and Mako 1.3.3. It was working with Mako 1.3.2.

@sgranjoux
Copy link
Author

This regression has been introduced by this commit e2606d5

@zzzeek
Copy link
Member

zzzeek commented May 13, 2024

whew OK it's not a regex issue, should be easy to fix unless you have a PR in mind?

sgranjoux pushed a commit to sgranjoux/mako that referenced this issue May 13, 2024
… flag

Fixes: sqlalchemy#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.
sgranjoux pushed a commit to sgranjoux/mako that referenced this issue May 13, 2024
… flag

Fixes: sqlalchemy#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.
sgranjoux pushed a commit to sgranjoux/mako that referenced this issue May 13, 2024
… flag

Fixes: sqlalchemy#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.
@sqla-tester
Copy link
Collaborator

Sébastien Granjoux has proposed a fix for this issue in the main branch:

Fix support comprehensions inside functions when use strict_undefined… https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants