f-string sytax error when importing macro in a template which filename is also a template
rafalkrupinski opened this issue · comments
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
Please paste the full traceback.
% 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
Any chance to move this forward?