Skip to content
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

Merged
merged 1 commit into from Jul 6, 2017
Merged

Conversation

@davidism
Copy link
Member

davidism commented Jan 20, 2017

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.

{{ 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:

  • unit tests
  • documentation
  • changelog

closes #664

@davidism davidism self-assigned this Jan 20, 2017
@davidism davidism requested a review from mitsuhiko Jan 20, 2017
jinja2/tests.py Outdated Show resolved Hide resolved
jinja2/tests.py Outdated Show resolved Hide resolved
@ThiefMaster
Copy link
Member

ThiefMaster commented Jan 20, 2017

Is it really a good idea to allow symbol operators in "normal" tests, i.e. for stuff like foo is == 'bar'? That looks completely awful to me to be honest. I'd rather allow an "extended set of tests" for the places where you use them as strings, i.e. select, reject and their ...attr variants.

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 {% if foo is == 'bar' %} as a syntax error.

jinja2/tests.py Outdated Show resolved Hide resolved
@davidism
Copy link
Member Author

davidism commented Jan 20, 2017

@ThiefMaster I thought it was a bit weird too, but I figured people would get confused by the inconsistency between is and select otherwise. The docs already call out equalto as being useless in is.

Since there was no expectation before that symbols could be used, we could just document them as a special case in the select docs, rather than in the general test docs.

Or we could still allow them in both locations, but only document them in select. I can open a PyCharm issue for the syntax, although we both know it could take a while to get in.

jinja2/tests.py Outdated Show resolved Hide resolved
@davidism davidism removed the request for review from mitsuhiko Jan 23, 2017
@davidism davidism force-pushed the operator-tests branch from 76f65de to 1a6c6ab Jul 6, 2017
@davidism
Copy link
Member Author

davidism commented Jul 6, 2017

Going to take out the is <= support, since I agree it looks weird. Here's the patch if we decide we need this later.

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':
@davidism davidism force-pushed the operator-tests branch from 1a6c6ab to 507369d Jul 6, 2017
add tests and aliases for all comparison operators
adjust docs to prefer short names for compare tests
closes #664
@davidism davidism force-pushed the operator-tests branch from 507369d to d62f050 Jul 6, 2017
@davidism davidism merged commit ca1abd4 into master Jul 6, 2017
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@davidism davidism deleted the operator-tests branch Jul 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.