-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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_test
is adjusted to allow comparison symbols as test names, so that they can be used in direct tests as well as filters.Todo:
closes #664