Skip to content

f-string sytax error when importing macro in a template which filename is also a template #1792

Description

@rafalkrupinski

With this template structure:

/templates/
+- {% jinja expr %}.jinja
+- macro_file.jina

When {% jinja expr %}.jinja imports a macro using 'from "macro_file.jinja" import my_macro' syntax, Environment.render fails with SyntaxError: f-string: invalid syntax error message.

Internally jinja concatenates the importing template filename into an f-string, which results with invalid f-string:

if l_0_print_helo is missing:
        l_0_print_helo = undefined(f"the template {included_template.__name__!r} (imported on line 1 in '{% if True %}a{%endif%}.txt.jinja') does not export the requested name 'print_helo'", name='print_helo')

Couldn't reproduce the error using import "macro_file.jinja" as m syntax.

Environment:

  • Python 3.10.7
  • Jinja 3.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions