Closed schittli closed 8 months ago
- ⛔ Basic behavior broke: If a template references a
{{ obj.property }}
and property does not exist, then:
- Jinja raises an
TemplateSyntaxError
exception- and it does not add
obj.property
it tometa.find_undeclared_variables
I can't reproduce raising TemplateSyntaxError
. Sounds like you have a syntax error that's not present in the example you've shown.
- ⛔ Basic behavior broke: If a template references a
{{ obj.property }}
and obj does not exist, then
- Jinja raises an
UndefinedError
exception- and it does not add
obj.property
it tometa.find_undeclared_variables
This and the previous described behavior about find_undeclared_variables
is correct. Like Python, Jinja parses the template into an AST, then produces a sequence of instructions to follow at runtime. At parsing, Python and Jinja know nothing about whether any given name will refer to an object that has further accessed attributes. It can know if the name itself is not defined within the template, but not further attribute access.
Here are two Python-only examples that demonstrate this. you can't tell from looking whether they will succeed, you must run them in context. This is because Python has very dynamic ways to work with attributes.
# Here we know what `a` and `b` are, but not whether `.value` would succeed for them.
a = ThingA()
b = ThingB()
a.value + b.value
# Here we don't know what `a` and `b` are, so we don't know if `.value` is valid either.
a.value + b.value
Hello @davidism thank you very much for your answer & help! I will try to re-test the TemplateSyntaxError issue and I will follow up.
Related to:
# Here we don't know what a and b are, so we don't know if .value is valid either.
a.value + b.value
I would be surprised if hasattr(object, propertyName)
could not check whether property .value
exists.
Therefore, there is probably no reason why a preprocessor cannot use the AST parser to check whether all variables are resolvable and thus save all errors in meta.find_undeclared_variables().
Thanks a lot, kind regards, Thomas
Hello
Thank you very much for sharing your great work!, it makes life with Python much better.
Intro
As a 'end-user' of Jinja, one would expect this most basic behavior: Regardless of whether one works in the template with
{{ variable }}
or with{{ obj.property }}
elements, Jinja behaves always the same if one of these elements does not exist. If Jinja handles missing references differently, then Jinja becomes tedious because one has to handle both errors in the templates and in the Python code differently.Describe the bug / summary
{{ variable }}
which does not exist, then:variable
it tometa.find_undeclared_variables
{{ obj.property }}
and property does not exist, then:TemplateSyntaxError
exceptionobj.property
it tometa.find_undeclared_variables
{{ obj.property }}
and obj does not exist, thenUndefinedError
exceptionobj.property
it tometa.find_undeclared_variables
Code to replicate the bug
To show the different jinja behavior, I have created this
RenderTemplate()
function which:The 4 tests
Now, we test 4 different situations of non-existent variables and properties. We use those variables / objects:
message = 'Good morning'
person = Person(prename = 'John')
'{{ message }} {{ person.prename }}'
'Good morning John'
'{{ unknwonVar }} {{ person.prename }}'
unknwonVar
does not exist'{{ unknwonVar }} John'
unknwonVar
'{{ message }} {{ person.name }}'
.name
does not existsTemplateSyntaxError
'{{ message }} {{ noperson.prename }}'
noperson
does not existUndefinedError
🙏 Please:
{{ var }}
does not exist.This code calls all 4 tests:
Thanks a lot, kind regards, Thomas