Skip to content

Bad comparison parsing for constants #1102

@areebbeigh

Description

@areebbeigh

Comparisons in jinja don't behave like comparisons in Python.

template = '''{{4 < 2 < 3}}
{{a < b < c}}'''
t = Template(template)
print(t.render(a = 4, b = 2, c = 3))

Expected output:

False
False

Output:

True
False

Python code:

4 < 2 < 3

Output: False

The problem lies in

jinja/jinja2/nodes.py

Lines 775 to 780 in 1af1205

try:
for op in self.ops:
new_value = op.expr.as_const(eval_ctx)
result = _cmpop_to_func[op.op](value, new_value)
value = new_value
except Exception:

where the result of the comparison depends only on the final operator result.

I would like to submit a PR for this.

  • Python version: 3.6.7
  • Jinja version: 2.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions