pallets / jinja

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

jinja filters converts none to string 'none' #2024

Open vladimir-vvalov opened 1 month ago

vladimir-vvalov commented 1 month ago

When argument value is None filters convert it to string 'None' then process it as string 'None'. But if this behaviour was correct, the expression {{ if none == 'None' }} should return True. But returns False. Also I didn't find any information about this behaviour in documentation: https://jinja.palletsprojects.com/en/3.1.x/templates/

code:

--test
{%- set value = none %}
value: {{ value }}

--now value is none and isn't string
check value is none: {{ true if value is none else false }}
check value is string: {{ true if value is string else false }}

--example with trim and length - none converts to string 'None'
value_trim: {{ value|trim }}
value_trim_length: {{ (value|trim)|length }}
check value_trim is none: {{ true if value|trim is none else false }}
check value_trim is string: {{ true if value|trim is string else false }}

--example with replace - none converts to string 'None'
replace_value: {{ value|replace('n','x') }}
check value_replace is none: {{ true if value|replace('n','x') is none else false }}
check value_replace is string: {{ true if value|replace('n','x') is string else false }}

--check if none == 'None'
check if none == 'None': {{ true if none == 'None' else false }}

compiled code:

--test
value: None

--now value is none and isn't string
check value is none: True
check value is string: False

--example with trim and length - none converts to string 'None'
value_trim: None
value_trim_length: 4
check value_trim is none: False
check value_trim is string: True

--example with replace - none converts to string 'None'
replace_value: Noxe
check value_replace is none: False
check value_replace is string: True

--check if none == 'None'
check if none == 'None': False

expected behavior: It was expected that if the argument is None, then the filter like trim() will also return None.

Thank you.

Environment:

kevin-brown commented 1 month ago

I should note this appears to happen on filters that are specially geared towards strings. I'm not sure what you'd otherwise expect to happen when you ask to trim None but I'm confident multiple people would have differing opinions.