diff --git a/CHANGES.rst b/CHANGES.rst index 7fb729763..4f5649415 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/jinja2/compiler.py b/src/jinja2/compiler.py index 91720c5f9..59711a3c2 100644 --- a/src/jinja2/compiler.py +++ b/src/jinja2/compiler.py @@ -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: diff --git a/tests/test_compile.py b/tests/test_compile.py index 42a773f21..7980fc796 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -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_{}")