diff --git a/CHANGES.rst b/CHANGES.rst index 36db0843a..fa2a6c6d9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,8 @@ Unreleased - Fix compiler error when checking if required blocks in parent templates are empty. :pr:`1858` +- 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.2 diff --git a/src/jinja2/compiler.py b/src/jinja2/compiler.py index 7dfac0a71..dab396a02 100644 --- a/src/jinja2/compiler.py +++ b/src/jinja2/compiler.py @@ -1122,12 +1122,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_{}")