Skip to content

Commit

Permalink
fix f-string syntax error in code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Oct 11, 2024
1 parent af054f3 commit cf62e59
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Unreleased
``Template.generate_async``. :pr:`1960`
- Avoid leaving async generators unclosed in blocks, includes and extends.
:pr:`1960`
- Fix f-string syntax error in code generation when importing a macro in a
template whose name contains curly braces. :issue:`1792`


Version 3.1.4
Expand Down
8 changes: 4 additions & 4 deletions src/jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,12 @@ def visit_FromImport(self, node: nodes.FromImport, frame: Frame) -> None:
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
self.indent()
message = (
"the template {included_template.__name__!r}"
f" (imported on {self.position(node)})"
f" does not export the requested name {name!r}"
'f"the template {included_template.__name__!r}" + '
f'"(imported on {self.position(node)})" + '
f'"does not export the requested name {name!r}"'
)
self.writeline(
f"{frame.symbols.ref(alias)} = undefined(f{message!r}, name={name!r})"
f"{frame.symbols.ref(alias)} = undefined({message}, name={name!r})"
)
self.outdent()
if frame.toplevel:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test_import_as_with_context_deterministic(tmp_path):
expect = [f"'bar{i}': " for i in range(10)]
found = re.findall(r"'bar\d': ", content)[:10]
assert found == expect


def test_import_as_with_curly_braces_in_template_name():
env = Environment(
loader=DictLoader(
{
"template_with_{}": "{% import 'macro' as m %}",
"macro": "{% macro m() %}{% endmacro %}",
}
)
)
env.get_template("template_with_{}")

0 comments on commit cf62e59

Please sign in to comment.