Skip to content

[BUG] Unexpected TypeError when using boolean test inside numeric expression #755

@eli-collins

Description

@eli-collins

I got an unexpected TypeError when rendering the template string: {% set x = None %}{{ 2 * (x == "foo")}}.

My best guess is that something in this fragment is being incorrectly compiled to python code.

Full test case:

>>> from jinja2 import Environment
>>> template = Environment().from_string('{% set x = None %}{{ 2 * (x == "foo")}}')
>>> template.render()
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

Expected behavior is that render would return "0".

Some other similar template strings:

  • {% set x = "bar" %}{{ 2 * (x == "foo") }} unexpectedly renders "False", rather than "0".
  • {% set x = "foo" %}{{ 2 * (x == "foo") }} unexpectedly renders "False", rather than "2".
  • yet {% set x = "foo" %}{{ (x == "foo") }} correctly renders "True".
  • and {{ 2 * True }} correctly renders "2".

Tested this with jinja 2.0 - 2.9.6, under python 2.7 & 3.5.

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