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

fix f-string syntax error in code generation #1852

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_{}")