pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NativeEnvironment casts integer strings to integer -- intended behavior?

rrauenza opened this issue · comments

When using NativeEnvironment, jinja seems to convert strings that look like integers to integers.

>>> import jinja2.nativetypes
>>> env = jinja2.nativetypes.NativeEnvironment()
>>> result = env.from_string('{{ x }}').render(x=2)
>>> type(result)
<class 'int'>
>>> result = env.from_string('{{ x }}').render(x='2')
>>> type(result)
<class 'int'>
>>> type(env.from_string('{{ x|string}}').render(x='2'))
<class 'int'>

I would expect in the second case that jinja would preserve that x is a string. Is this behavior intentional?

Environment:

  • Python version: 3.12.0
  • Jinja version: 3.1.2

(also does it with float like strings... and bools...)

Yes, this is intended.

Is this already exposed in the docs? I didn't see it in the NativeEnvironment docs and could propose a change there if that would be helpful.