Allow using comparison operator symbols as tests#665
Conversation
|
Is it really a good idea to allow symbol operators in "normal" tests, i.e. for stuff like This would also add the advantage that editors and IDEs won't have to deal with new syntax when highlighting/checking Jinja code - I'm quite quire that PyCharm is not the only IDE that currently shows |
|
@ThiefMaster I thought it was a bit weird too, but I figured people would get confused by the inconsistency between Since there was no expectation before that symbols could be used, we could just document them as a special case in the Or we could still allow them in both locations, but only document them in |
|
Going to take out the diff --git a/jinja2/parser.py b/jinja2/parser.py
index 6d1fff6a..8741520a 100644
--- a/jinja2/parser.py
+++ b/jinja2/parser.py
@@ -825,10 +825,19 @@ def parse_test(self, node):
negated = True
else:
negated = False
- name = self.stream.expect('name').value
- while self.stream.current.type == 'dot':
- next(self.stream)
- name += '.' + self.stream.expect('name').value
+
+ # If the next token is a comparison operator, treat it as a name.
+ # This allows using the operator symbols in the select filter without
+ # breaking unexpectedly when used in a test node.
+ if self.stream.current.type in _compare_operators:
+ name = next(self.stream).value
+ else:
+ name = self.stream.expect('name').value
+
+ while self.stream.current.type == 'dot':
+ next(self.stream)
+ name += '.' + self.stream.expect('name').value
+
dyn_args = dyn_kwargs = None
kwargs = []
if self.stream.current.type == 'lparen': |
add tests and aliases for all comparison operators adjust docs to prefer short names for compare tests closes #664
I added the Python short names for the operators as well as the symbols. I kept the long names for the three existing tests.
parse_testis adjusted to allow comparison symbols as test names, so that they can be used in direct tests as well as filters.{{ x is <= 5 }} isn't very useful, but it's allowed for consistency {{ x is le 5 }} {{ x|select('<=', 5) }} {{ x|select('le', 5) }}Todo:
closes #664