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 1 commit into from Jul 6, 2017


Copy link

@davidism 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) }}


  • 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/ Outdated Show resolved Hide resolved
jinja2/ Outdated Show resolved Hide resolved
Copy link

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/ Outdated Show resolved Hide resolved
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/ Outdated Show resolved Hide resolved
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/ b/jinja2/
index 6d1fff6a..8741520a 100644
--- a/jinja2/
+++ b/jinja2/
@@ -825,10 +825,19 @@ def parse_test(self, node):
             negated = True
             negated = False
-        name ='name').value
-        while == 'dot':
-            next(
-            name += '.' +'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 in _compare_operators:
+            name = next(
+        else:
+            name ='name').value
+            while == 'dot':
+                next(
+                name += '.' +'name').value
         dyn_args = dyn_kwargs = None
         kwargs = []
         if == 'lparen':

add tests and aliases for all comparison operators
adjust docs to prefer short names for compare tests
closes #664
@davidism davidism merged commit ca1abd4 into master Jul 6, 2017
1 check passed
@davidism davidism deleted the operator-tests branch Jul 6, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

Successfully merging this pull request may close these issues.

Create tests for all Operators
3 participants