pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.6k forks source link

NativeEnvironment casts integer strings to integer -- intended behavior? #1904

Closed rrauenza closed 10 months ago

rrauenza commented 10 months ago

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:

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

davidism commented 10 months ago

Yes, this is intended.

rrauenza commented 10 months ago

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.