Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
rafalkrupinski opened this issue Jan 15, 2023 · 3 comments
Milestone

Comments

@rafalkrupinski
Copy link

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
rafalkrupinski added a commit to rafalkrupinski/jinja_1792 that referenced this issue Jan 15, 2023
@davidism
Copy link
Member

Please paste the full traceback.

@rafalkrupinski
Copy link
Author

rafalkrupinski commented Jan 16, 2023

project that demonstrates it

% poetry run python -m template_in_filename
/Users/matte/Documents/Projects/jinja_1792/template_in_filenameTraceback (most recent call last):
  File "/Users/matte/.pyenv/versions/3.10.7/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Users/matte/.pyenv/versions/3.10.7/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/Users/matte/.pyenv/versions/3.10.7/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/Users/matte/Documents/Projects/jinja_1792/template_in_filename/__init__.py", line 10, in <module>
    environment.get_template('{% if True %}a{%endif%}.txt.jinja').render(var=True)
  File "/Users/matte/Library/Caches/pypoetry/virtualenvs/template-in-filename-37afmHoY-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 1010, in get_template
    return self._load_template(name, globals)
  File "/Users/matte/Library/Caches/pypoetry/virtualenvs/template-in-filename-37afmHoY-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
  File "/Users/matte/Library/Caches/pypoetry/virtualenvs/template-in-filename-37afmHoY-py3.10/lib/python3.10/site-packages/jinja2/loaders.py", line 138, in load
    code = environment.compile(source, name, filename)
  File "/Users/matte/Library/Caches/pypoetry/virtualenvs/template-in-filename-37afmHoY-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 766, in compile
    return self._compile(source, filename)
  File "/Users/matte/Library/Caches/pypoetry/virtualenvs/template-in-filename-37afmHoY-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 704, in _compile
    return compile(source, filename, "exec")  # type: ignore
  File "/Users/matte/Documents/Projects/jinja_1792/template_in_filename/templates/{% if True %}a{%endif%}.txt.jinja", line 15
    (% if True %)
     ^
SyntaxError: f-string: invalid syntax

@rafalkrupinski
Copy link
Author

Any chance to move this forward?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants