Skip to content

Error with nested list comprehension when using strict_undefined=True #418

Closed

Description

@sgranjoux

Here is a failing sample

from mako.template import Template

text = """
<%
b = {}
bs = sorted(b, key=lambda b: sum(f for s in b.values() for f in s.values()))
%>
"""

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

This produces the following error

Traceback (most recent call last):
  File "memory:0x7164e2d38380", line 28, in render_body
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 106, in __getitem__
    return builtins.__dict__[key]
           ~~~~~~~~~~~~~~~~~^^^^^
KeyError: 's'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sgr/Projects/Ganymede/digital/design/top_xt018_fx/titi.py", line 11, in <module>
    out = tmpl.render()
          ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/mako/template.py", line 434, in render
    return runtime._render(self, self.callable_, args, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 874, in _render
    _render_context(
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 916, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/usr/local/lib/python3.12/dist-packages/mako/runtime.py", line 943, in _exec_template
    callable_(context, *args, **kwargs)
  File "memory:0x7164e2d38380", line 30, in render_body
NameError: 's' is not defined

Activity

  1. sqla-tester commented on Apr 10, 2025

    @sqla-tester
    Collaborator

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

    Fix undeclared variables in nested list comprehension with strict_undefined https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/5861

  2. zzzeek commented on Apr 10, 2025

    @zzzeek
    Member

    OK. I guess you want a release too :). it's thursday so that's good, in case this causes breakage (lots of PRs do).

  3. zzzeek commented on Apr 10, 2025

    @zzzeek
    Member

    1.3.10 is out

  4. sgranjoux commented on Apr 10, 2025

    @sgranjoux
    Author

    Indeed a release was nice. Thank you, that's really fast.

  5. added a commit that references this issue on Apr 11, 2025
    0fb93ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglexer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions